[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

MySQL refuses to use index

Asked by cyberstalker in MySQL Server

Tags: MySQL 5.0

I am having trouble with a query where MySQL does not use an index effectively. Even forcing MySQL to use the index does not solve the problem. The query in question you can find at the bottom, in the code box. I have also put an explain of the query and a DESCRIBE of the tables involved.

MySQL should, of course, be using the index called 'zipcode'.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
SELECT  CONCAT('NRC', `nrc`.`id`)                       AS  `id`,
        `nrc`.`name_initials`                           AS  `name_initials`,
        `nrc`.`name_suffix`                             AS  `name_suffix`,
        `nrc`.`name_last`                               AS  `name_last`,
        `nrc`.`sex`                                     AS  `sex`,
        `location_ptt`.`street_short`                   AS  `street`,
        `nrc`.`number`                                  AS  `number`,
        `nrc`.`number_app`                              AS  `number_app`,
        `nrc`.`zipcode`                                 AS  `zipcode`,
        `nrc`.`city`                                    AS  `city`,
        `nrc`.`phone`                                   AS  `phone`,
        `nrc`.`email`                                   AS  `email`,
        CASE
            WHEN    `nrc`.`start_date`  =   `mScoreCard`.`date`
            THEN    ';;'
            ELSE    DATE_FORMAT(`nrc`.`start_date`, '%d;%m;%Y')
        END                                             AS  `start_date`,
        `nrc`.`pay_method`                              AS  `pay_method`,
        `nrc`.`type`                                    AS  `type`,
        `nrc`.`account`                                 AS  `account`,
        DATE_FORMAT(`nrc`.`dob`, '%d;%m;%Y')            AS  `dob`,
        DATE_FORMAT(`mScoreCard`.`date`, '%d-%m-%Y')    AS  `score_date`,
        `mScoreCard`.`route_name`                       AS  `location`
FROM    `nrc`
JOIN    `location_ptt`
ON      `nrc`.`zipcode`                                 =   `location_ptt`.`zipcode`
AND     `nrc`.`number`                                  BETWEEN `location_ptt`.`number_min` AND `location_ptt`.`number_max`
AND     `location_ptt`.`even`                           =   (`nrc`.`number` + 1) MOD 2
JOIN    `mScoreCard`
ON      `nrc`.`fk_mSCid`                                =   `mScoreCard`.`mSCid`
WHERE   `id`                                            =   601; 
Explain:
+----+-------------+--------------+-------+--------------------------+---------+---------+-------+--------+-------------+
| id | select_type | table        | type  | possible_keys            | key     | key_len | ref   | rows   | Extra       |
+----+-------------+--------------+-------+--------------------------+---------+---------+-------+--------+-------------+
|  1 | SIMPLE      | nrc          | const | PRIMARY,fk_mSCid,zipcode | PRIMARY | 4       | const |      1 |             | 
|  1 | SIMPLE      | mScoreCard   | const | PRIMARY                  | PRIMARY | 4       | const |      1 |             | 
|  1 | SIMPLE      | location_ptt | ref   | even                     | even    | 1       | const | 306070 | Using where | 
+----+-------------+--------------+-------+--------------------------+---------+---------+-------+--------+-------------+
3 rows in set (0.00 sec) 
DESCRIBE nrc;
+---------------+----------------------------------------+------+-----+---------+----------------+
| Field         | Type                                   | Null | Key | Default | Extra          |
+---------------+----------------------------------------+------+-----+---------+----------------+
| id            | int(7) unsigned zerofill               | NO   | PRI | NULL    | auto_increment | 
| fk_mSCid      | int(11)                                | NO   | MUL | NULL    |                | 
| fk_pid        | int(11)                                | NO   |     | NULL    |                | 
| sex           | enum('M','V','')                       | NO   |     | NULL    |                | 
| name_initials | varchar(20)                            | NO   |     | NULL    |                | 
| name_suffix   | varchar(15)                            | NO   |     | NULL    |                | 
| name_last     | varchar(40)                            | NO   |     | NULL    |                | 
| dob           | date                                   | NO   |     | NULL    |                | 
| number        | int(10) unsigned                       | NO   |     | NULL    |                | 
| number_app    | varchar(7)                             | NO   |     | NULL    |                | 
| zipcode       | char(6)                                | NO   | MUL | NULL    |                | 
| city          | varchar(30)                            | NO   |     | NULL    |                | 
| phone         | varchar(10)                            | NO   |     | NULL    |                | 
| email         | varchar(60)                            | NO   |     | NULL    |                | 
| account       | varchar(9)                             | NO   |     | NULL    |                | 
| start_date    | date                                   | NO   |     | NULL    |                | 
| pay_method    | tinyint(1)                             | YES  |     | NULL    |                | 
| type          | enum('W13','W26','W52','M3','M6','Y1') | NO   |     | NULL    |                | 
| premium       | enum('7009')                           | NO   |     | NULL    |                | 
| submitted     | tinyint(1)                             | NO   |     | 0       |                | 
+---------------+----------------------------------------+------+-----+---------+----------------+
20 rows in set (4.24 sec) 
DESCRIBE location_ptt;
+--------------+---------------------+------+-----+---------+-------+
| Field        | Type                | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+-------+
| zipcode      | char(6)             | NO   | MUL | NULL    |       | 
| even         | tinyint(3) unsigned | NO   | MUL | NULL    |       | 
| number_min   | int(10) unsigned    | NO   |     | NULL    |       | 
| number_max   | int(10) unsigned    | NO   |     | NULL    |       | 
| fk_cid       | int(10) unsigned    | NO   | MUL | NULL    |       | 
| street_short | varchar(31)         | NO   |     | NULL    |       | 
| street_long  | varchar(63)         | NO   |     | NULL    |       | 
| fk_mid       | int(10) unsigned    | NO   |     | NULL    |       | 
| fk_pid       | tinyint(3) unsigned | NO   |     | NULL    |       | 
| latitude     | float               | YES  |     | NULL    |       | 
| longitude    | float               | YES  |     | NULL    |       | 
+--------------+---------------------+------+-----+---------+-------+
11 rows in set (5.72 sec)
[+][-]11/03/09 08:01 PM, ID: 25736291Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/03/09 11:31 PM, ID: 25737150Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 02:44 AM, ID: 25738011Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 02:48 AM, ID: 25738022Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 02:53 AM, ID: 25738045Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 03:23 AM, ID: 25738204Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 07:01 AM, ID: 25739973Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 07:31 AM, ID: 25740293Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 07:42 AM, ID: 25740424Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 07:44 AM, ID: 25740446Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 08:24 AM, ID: 25740938Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 08:33 AM, ID: 25741043Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/07/09 01:21 PM, ID: 25768107Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/10/09 02:47 AM, ID: 25784108Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/10/09 05:32 AM, ID: 25785114Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/10/09 07:02 AM, ID: 25786028Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/17/09 06:47 AM, ID: 25840089Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/17/09 09:15 PM, ID: 25846744Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625