Forum Moderators: open

Message Too Old, No Replies

Indexes

         

dougmcc1

5:45 am on Dec 8, 2006 (gmt 0)

10+ Year Member



What would an ideal index look like for the following query?

$query="SELECT field1,field2,field3,field4,field5 FROM table WHERE field6 LIKE '%$value1%' AND field7!=''";

And this query:

$query="SELECT field1,field2,field3,field4,field5 FROM table WHERE field6='$value1' AND field7!=''";

Or this query:

$query="SELECT field1,field2,field3,field4,field5 FROM table WHERE field6='$value1' OR field7='$value2';

Or this one:

$query="SELECT field1,field2,field3,field4,field5 FROM table WHERE field6='$value1';

Would they all use different indexes or would it be best if they used the same one? What if I had other queries that selected different fields? Which field would I put the index on and what other fields do I need to add to the index(es)?

How would mysql know which index to use if many of them had the same fields added to them? Examples would be great.

Thanks.

phranque

10:27 am on Dec 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the index isn't used in the select list, it's used in the where clause.
you could do a composite (multi-column) index on field 6 & field 7 which would work for all these queries.
please see this for a clear example:
[dev.mysql.com...]

FalseDawn

4:38 pm on Dec 8, 2006 (gmt 0)

10+ Year Member




$query="SELECT field1,field2,field3,field4,field5 FROM table WHERE field6 LIKE '%$value1%' AND field7!=''";

Indexes won't help with this particular query, since you are matching with a wildcard at the start of the field value.

dougmcc1

4:55 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



Thanks for the replies. I don't even need to use the wildcard so I'll just modify my database and adjust the query to look for an exact value.

Thanks again.