isync has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

From my perl script (cheap excuse to ask something about SQL here..) I am accessing a table where I use various Indexes to optimize performance.

I know that it is possible to have a setup where an INDEX covers one field. I use indexes this way: having fieldA, fieldB, with an index for fieldA and another INDEX for fieldB, for example.

But what I've just discovered is that it's also possible to have one INDEX, which covers fieldA and(!) fieldB. Now, where is the difference, perfomancewise?

A quick hint would be nice! And sorry again for being OT...

Replies are listed 'Best First'.
Re: OT: Indexes in MySQL
by jeffa (Bishop) on Jan 20, 2009 at 21:17 UTC
Re: OT: Indexes in MySQL
by monarch (Priest) on Jan 20, 2009 at 21:19 UTC
    Please see the MySQL document page on multiple column indexes.

    Essentially performance for the single combined index vs two separate indices (not sure why they are referred to as indexes) is the same when you query using the first key or both keys.

    The combined index is only effective if you don't plan on attempting to query using only the second key. Order of keys is important in a combined index.

Re: OT: Indexes in MySQL
by kyle (Abbot) on Jan 20, 2009 at 21:20 UTC

    If you sort or search on both fieldA and fieldB frequently, a single index on the both of them will work better than one index for each of them. Also, if you have an index which covers (A, B, C), that can also be used just like an index on (A, B) or an index on A alone, but not B alone or C alone.

      If you sort or search on both fieldA and fieldB frequently, a single index on the both of them will work better than one index for each of them

      I've found out the hard way that this isn't always true -- if you're using inequality matching on both columns, and they both vary greatly, then you're often better using two separate indexes.

      In my particular case, I was testing for overlap of the time of a series of telescope images. (about 10M records). Because of the nature of the fields being searched on (start time and send time; essentially infinitely variable), the compound index was useless for the second field and was ignored. Two indexes gave better results, but depending on the size of the range being asked for, no indexes were better for excessively large matches.

      (although, I got even better performance by using the knowledge that the end date was always within 1 minute of the start date, so isolating the range using just a single field's index, and then _not_ having an index on the second field for the second weeding.)

      ...

      Anyway, the point was -- nothing's absolute when it comes to tuning. Use whatever benchmarking tools you have, test a few indexing strategies -- sometimes, you'll want to use hints or the equivalent to avoid indexes for some queries. And as nothing's absolute -- in many cases, you won't need to do this unless things are taking too long.

Re: OT: Indexes in MySQL
by isync (Hermit) on Jan 20, 2009 at 21:25 UTC
    Thanks everyone! Enough OT for today! ;-)
      It was too much OT for 10 years ;)
        ;-)