why mysql choose a bad query
why mysql choose a bad query
am 05.11.2010 05:48:37 von Changying Li
CREATE TABLE `abc` (
`threadid` int(11) NOT NULL AUTO_INCREMENT COMMENT '主é¢id',
`thread_type_id` int(11) DEFAULT NULL COMMENT '主é¢ç±»å«è¡¨id',
`forumid` smallint(6) DEFAULT NULL COMMENT '主é®id',
`title` varchar(250) DEFAULT NULL COMMENT 'æ é¢',
`lastpost` int(11) DEFAULT NULL COMMENT 'æååå¤æ¶é´',
`open` smallint(6) DEFAULT NULL COMMENT 'æ¯å¦å¼æ¾ï¼0为ä¸å¼æ¾,1为å¼æ¾,10为被移å¨ç',
`replycount` int(11) DEFAULT NULL COMMENT 'åå¸æ»æ°',
`postusername` varchar(100) DEFAULT NULL COMMENT 'å表人å称',
`postuserid` int(11) DEFAULT NULL COMMENT 'å表è
id',
`lastpostid` int(11) DEFAULT NULL,
`lastposter` varchar(100) DEFAULT NULL COMMENT 'æååå¤äºº',
`lastpostuserid` int(11) DEFAULT NULL,
`dateline` int(11) DEFAULT NULL COMMENT 'ä¿¡æ¯å表æ¶é´',
`views` int(11) DEFAULT NULL COMMENT 'æ¥ç次æ°',
`visible` smallint(6) DEFAULT NULL COMMENT 'æ¯å¦ææ,0为æªå®¡æ ¸,1ä¸ºå®¡æ ¸,2为软å é¤,3为è 稿箱',
`sticky` tinyint(1) DEFAULT NULL COMMENT 'æ¯å¦ç½®é¡¶',
`goodnees` tinyint(1) DEFAULT NULL COMMENT '1为精å,0为éç²¾å,é»è®¤ä¸º0',
`votenum` smallint(6) DEFAULT NULL COMMENT 'æ票个æ°',
`votetotal` int(11) DEFAULT NULL COMMENT 'æ票æ»å',
`attach` smallint(6) DEFAULT NULL COMMENT 'é件个æ°',
`hiddencount` int(11) DEFAULT NULL COMMENT 'æªå®¡æ ¸æç« æ»æ°',
`deletedcount` smallint(6) DEFAULT NULL COMMENT '被å é¤çåå¤æ»æ°',
`pid` int(11) DEFAULT NULL COMMENT 'å½openä¸ä¸º10çæ¶åå®ä»£è¡¨æ票id,å½opençäº10æ ¶å为被移å¨çå¸åçid',
PRIMARY KEY (`threadid`),
KEY `dateline` (`dateline`),
KEY `forumid_2` (`forumid`,`thread_type_id`,`visible`,`sticky`,`dateline`),
KEY `forumid` (`forumid`,`visible`,`sticky`,`dateline`)
) ENGINE=InnoDB AUTO_INCREMENT=660 DEFAULT CHARSET=utf8;
mysql> explain SELECT * FROM `abc` WHERE `forumid` = 25 AND `visible` = 1 AND `sticky` = 0 order by dateline \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: abc
type: ref
possible_keys: forumid_2,forumid
key: forumid_2
key_len: 3
ref: const
rows: 24
Extra: Using where; Using filesort
1 row in set (0.00 sec)
why it choose forumid_2, not forumid ?
--
Thanks & Regards
Changying Li
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
Re: why mysql choose a bad query
am 05.11.2010 08:21:35 von Todd Lyons
2010/11/4 Changying Li :
> =A0PRIMARY KEY (`threadid`),
> =A0KEY `dateline` (`dateline`),
> =A0KEY `forumid_2` (`forumid`,`thread_type_id`,`visible`,`sticky`,`dateli=
ne`),
> =A0KEY `forumid` (`forumid`,`visible`,`sticky`,`dateline`)
> ) ENGINE=3DInnoDB AUTO_INCREMENT=3D660 DEFAULT CHARSET=3Dutf8;
>
> mysql> explain =A0SELECT * =A0FROM `abc` WHERE `forumid` =3D 25 AND `visi=
ble` =3D 1 AND `sticky` =3D 0 order by dateline \G
> possible_keys: forumid_2,forumid
> =A0 =A0 =A0 =A0 =A0key: forumid_2
> Extra: Using where; Using filesort
>
> why it choose forumid_2, not forumid ?
5.0 docs online say:
With EXPLAIN SELECT ... ORDER BY, you can check whether MySQL can use
indexes to resolve the query. It cannot if you see Using filesort in
the Extra column. See Section 7.2.1, =93Optimizing Queries with
EXPLAIN=94.
Your query is using filesort, so it cannot according to the above statement=
..
The docs also say in section 7.3.1.11 that it might not use an index if:
The key used to fetch the rows is not the same as the one used in the
ORDER BY...
I'm curious, if you change the SELECT to a few named fields instead of
*, does it affect the key choice? If you only select on fields in the
key (i.e. a covering index) does it still choose what you consider to
be the wrong key?
--=20
Regards...=A0 =A0 =A0 Todd
I seek the truth...it is only persistence in self-delusion and
ignorance that does harm.=A0 -- Marcus Aurealius
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgcdmg-mysql-2@m.gmane.o rg
Re: why mysql choose a bad query
am 05.11.2010 08:46:56 von Changying Li
yes, I tried :
mysql> explain SELECT forumid,visible,sticky,dateline FROM `abc` WHERE `forumid` = 25 AND `visible` = 1 AND `sticky` = 0 order by dateline \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: abc
type: ref
possible_keys: forumid_2,forumid
key: forumid
key_len: 8
ref: const,const,const
rows: 24
Extra: Using where; Using index
1 row in set (0.00 sec)
and tried :
mysql> explain select threadid,thread_type_id,forumid,title,lastpost,open,replycou nt,postusername,postuserid,lastpostid,lastposter,lastpostuse rid,dateline,views,visible,sticky,goodnees,votenum,votetotal ,attach,hiddencount,deletedcount,pid from abc where `forumid` = 25 AND `visible` = 1 AND `sticky` = 0 order by dateline \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: abc
type: ref
possible_keys: forumid_2,forumid
key: forumid_2
key_len: 3
ref: const
rows: 24
Extra: Using where; Using filesort
1 row in set (0.00 sec)
but why ?
Must I use it by force index ?
Todd Lyons writes:
> 2010/11/4 Changying Li :
>> Â PRIMARY KEY (`threadid`),
>> Â KEY `dateline` (`dateline`),
>> Â KEY `forumid_2` (`forumid`,`thread_type_id`,`visible`,`sticky`,`dateline`),
>> Â KEY `forumid` (`forumid`,`visible`,`sticky`,`dateline`)
>> ) ENGINE=InnoDB AUTO_INCREMENT=660 DEFAULT CHARSET=utf8;
>>
>> mysql> explain  SELECT *  FROM `abc` WHERE `forumid` = 25 AND `visible` = 1 AND `sticky` = 0 order by dateline \G
>> possible_keys: forumid_2,forumid
>> Â Â Â Â Â key: forumid_2
>> Extra: Using where; Using filesort
>>
>> why it choose forumid_2, not forumid ?
>
> 5.0 docs online say:
>
> With EXPLAIN SELECT ... ORDER BY, you can check whether MySQL can use
> indexes to resolve the query. It cannot if you see Using filesort in
> the Extra column. See Section 7.2.1, âOptimizing Queries with
> EXPLAINâ.
>
> Your query is using filesort, so it cannot according to the above statement.
>
> The docs also say in section 7.3.1.11 that it might not use an index if:
>
> The key used to fetch the rows is not the same as the one used in the
> ORDER BY...
>
> I'm curious, if you change the SELECT to a few named fields instead of
> *, does it affect the key choice? If you only select on fields in the
> key (i.e. a covering index) does it still choose what you consider to
> be the wrong key?
> --
> Regards...   Todd
> I seek the truth...it is only persistence in self-delusion and
> ignorance that does harm. -- Marcus Aurealius
--
Thanks & Regards
Changying Li
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=gcdmg-mysql-2@m.gmane.org
Re: why mysql choose a bad query
am 05.11.2010 13:25:56 von Steven Staples
On Fri, 2010-11-05 at 15:46 +0800, Changying Li wrote:
> yes, I tried :
> mysql> explain SELECT forumid,visible,sticky,dateline FROM `abc` WHERE =
`forumid` =3D 25 AND `visible` =3D 1 AND `sticky` =3D 0 order by dateline \=
G
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: abc
> type: ref
> possible_keys: forumid_2,forumid
> key: forumid
> key_len: 8
> ref: const,const,const
> rows: 24
> Extra: Using where; Using index
> 1 row in set (0.00 sec)
>=20
> and tried :
> mysql> explain select threadid,thread_type_id,forumid,title,lastpost,open=
,replycount,postusername,postuserid,lastpostid,lastposter,la stpostuserid,da=
teline,views,visible,sticky,goodnees,votenum,votetotal,attac h,hiddencount,d=
eletedcount,pid from abc where `forumid` =3D 25 AND `visible` =3D 1 AND `s=
ticky` =3D 0 order by dateline \G
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: abc
> type: ref
> possible_keys: forumid_2,forumid
> key: forumid_2
> key_len: 3
> ref: const
> rows: 24
> Extra: Using where; Using filesort
> 1 row in set (0.00 sec)
>=20
>=20
> but why ?
>=20
> Must I use it by force index ?
>=20
>=20
>=20
> Todd Lyons writes:
>=20
> > 2010/11/4 Changying Li :
> >> PRIMARY KEY (`threadid`),
> >> KEY `dateline` (`dateline`),
> >> KEY `forumid_2` (`forumid`,`thread_type_id`,`visible`,`sticky`,`datel=
ine`),
> >> KEY `forumid` (`forumid`,`visible`,`sticky`,`dateline`)
> >> ) ENGINE=3DInnoDB AUTO_INCREMENT=3D660 DEFAULT CHARSET=3Dutf8;
> >>
> >> mysql> explain SELECT * FROM `abc` WHERE `forumid` =3D 25 AND `visib=
le` =3D 1 AND `sticky` =3D 0 order by dateline \G
> >> possible_keys: forumid_2,forumid
> >> key: forumid_2
> >> Extra: Using where; Using filesort
> >>
> >> why it choose forumid_2, not forumid ?
> >
> > 5.0 docs online say:
> >
> > With EXPLAIN SELECT ... ORDER BY, you can check whether MySQL can use
> > indexes to resolve the query. It cannot if you see Using filesort in
> > the Extra column. See Section 7.2.1, â=9COptimizing Queries with
> > EXPLAINâ=9D.
> >
> > Your query is using filesort, so it cannot according to the above state=
ment.
> >
> > The docs also say in section 7.3.1.11 that it might not use an index if=
:
> >
> > The key used to fetch the rows is not the same as the one used in the
> > ORDER BY...
> >
> > I'm curious, if you change the SELECT to a few named fields instead of
> > *, does it affect the key choice? If you only select on fields in the
> > key (i.e. a covering index) does it still choose what you consider to
> > be the wrong key?
> > --=20
> > Regards... Todd
> > I seek the truth...it is only persistence in self-delusion and
> > ignorance that does harm. -- Marcus Aurealius
>=20
> --=20
>=20
> Thanks & Regards
>=20
> Changying Li
>=20
>=20
what if you added the "dateline" into the where clause? i understand
that the indexs are used left to right, but could it just be the order
by is the issue?
I dunno honestly, but it does apear that forumid_2 is a better choice
based on the key_len and rows that the explain shows...
Steve
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe: http://lists.mysql.com/mysql?unsub=3Dgcdmg-mysql-2@m.gmane.o rg