Author Topic: index question  (Read 309 times)

0 Members and 2 Guests are viewing this topic.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
index question
« on: March 10, 2010, 03:11:30 PM »
Can someone give me examples on when to use a multi column index vs two single column indexes?

thanks.

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: index question
« Reply #1 on: March 15, 2010, 12:49:24 PM »
If you're just using constants for both fields, or are querying on one and sorting on the other, this can be much faster for MyISAM tables.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #2 on: March 15, 2010, 04:33:48 PM »
ok so for queries where you are using both fields in the where clause, it would be faster to have one index on both fields compared to one indexes on each field?

All of our tables are MyISAM.

Offline ajlisowski

  • Enthusiast
    • View Profile
Re: index question
« Reply #3 on: March 15, 2010, 05:07:06 PM »
From my experience if you have one index on each field, as opposed to having one on both, it will not use the index.

I recently had an issue where I was not indexing on both fields (and instead had two indexes on each individual field) and my query was taking literally 90 seconds. I added the index on both fields and it cut it down to like 800ms.

Offline Mchl

  • Guru
  • Freak!
  • *
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • BOINC@Poland
Re: index question
« Reply #4 on: March 15, 2010, 05:15:04 PM »
This increases performace also for InnoDB. And another thing to consider is order of columns in index defintion.

WHERE column1 = ? AND column2 = ? will use index created on (column1,column2) but not on (column2, column1).
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Code: [Select]
brugere i panik

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: index question
« Reply #5 on: March 19, 2010, 08:46:31 AM »
Well, 5.1 has index_merge, so you can sometimes get around the "one index per table" issue.

Also:

This increases performace also for InnoDB. And another thing to consider is order of columns in index defintion.

WHERE column1 = ? AND column2 = ? will use index created on (column1,column2) but not on (column2, column1).

That's not actually true -- the optimizer will handle this quite easily.  The order only matter when there are not equality constraints; in that case, the constant needs to go first.  That is:

WHERE column1 = ? AND column2 >= ?  will use the index

BUT

WHERE column1 >= ? AND column2 = ?  won't.
« Last Edit: March 19, 2010, 08:47:02 AM by fenway »
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #6 on: March 19, 2010, 12:10:51 PM »
Thanks everyone for the feedback.

What's the one index per table issue?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: index question
« Reply #7 on: March 22, 2010, 04:31:27 PM »
What's the one index per table issue?

MySQL will pick the "best" one to use, and it's not always the most desirable.  MySQL 5 has index_merge, which will sometime generate a pseudo-index that can use multiple, but again, not always reliable.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #8 on: March 22, 2010, 04:43:22 PM »
so if i have a table with multiple single column indexes and i do something like:

select * from table where column1 = value1 and column2 = value2 etc etc.

and there is an index for column 1 and column 2, MySQL will only use the index on one of them and not both?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: index question
« Reply #9 on: March 22, 2010, 04:44:53 PM »
That's correct -- EXPLAIN will tell you which one it picked, or if it decided to merge them.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #10 on: March 22, 2010, 04:56:22 PM »
Thanks Fenway. looks like i need to revamp a ton of indexes. I didn't know it could only use one =(

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #11 on: March 22, 2010, 05:00:31 PM »
Ok so is that one index per table? how would that work a multiple table query?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: index question
« Reply #12 on: March 22, 2010, 11:00:59 PM »
Ok so is that one index per table? how would that work a multiple table query?

Still one each.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline schillyTopic starter

  • Devotee
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: index question
« Reply #13 on: March 23, 2010, 12:32:17 PM »
ok cool. thanks.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.