Jump to content

Creating indes makes no difference in SELECT statement


MockY

Recommended Posts

I have a table with almost 800,000 rows. Without any indexes (just an auto incremented Id), a simple SELECT statement takes almost 2 seconds.

SELECT Id, Number FROM clients WHERE Number=8596;

 

Creating an index on Number makes no difference. The query takes just as long. Why on earth is that? I figured that if you create an index on the field you have in the WHERE clause, that the query should become so much faster.

The Number field is a VARCHAR(11) by the way.

 

EDIT: For some reason I tried to enclode the number in the where clause in single quotes, and then the query took 0.00 seconds. I've been trying to figure this out all day as I am creating a report file with just 17,000 records, but it has been taken hours and it's not even half done. I made this single quote change and the file was created in a few seconds.

 

Now begs the question, why this enormous change?

 

EDIT2:

I did an explain on the last query and ended up with this

+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+

 

Without the quotes, I get this

+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | clients  | ALL  | number_index | NULL | NULL    | NULL | 725713 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+--------+-------------+

 

Even though the quoted query is efficient as heck and creates the results I want, the explain command sure came back with some odd data that makes me kind of confused.

Any help with shedding some light on my problem would be much appreciated.

 

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.