Jump to content

mySql Indexes to Optimize Applications


blackcell

Recommended Posts

I can create indexes but, how do you utilize them to optimize look-ups? Is it something that happens behind the scenes or do you have to tailor your queries to utilize them?

 

<?php
$sqlQuery = "SELECT * FROM `sqlTable` WHERE `userDepartment` = 'ENG'";
?>

Would I need to modify the example query above to optimize the look-up when there are 5000+ records?

 

Thanks for any help.

Link to comment
Share on other sites

I see...

I found something interesting and I have a theory as to why it happens.

I have a report that will query all records with field1 containing the char Y.

The possibilities for this field are Y or N.

This sql query takes FOREVER to run:

<?php
$sqlQuery = "SELECT * FROM `table` WHERE `field1` != 'N'";
?>

This sql query takes 20 seconds to rip through 4000+ records:

<?php
$sqlQuery = "SELECT * FROM `table` WHERE `field1` = 'Y'";
?>

I have an index on field1

 

Why is there such a time difference? Is it because when looking for != 'N' it leaves the possibility for everything else where looking for ='Y' will know what record to start on the index and rule all other possibilities out?

 

Link to comment
Share on other sites

I don't know your table structure but 20 seconds ain't exactly ripping.

SELECT COUNT(*) FROM sales;
+----------+
| COUNT(*) |
+----------+
|    10917 |
+----------+

SELECT * FROM sales WHERE prodcode <> '610';
10395 rows in set (0.13 sec)

SELECT * FROM sales WHERE prodcode = '610';
522 rows in set (0.02 sec)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.