我最近做一个东西,用到了mysql做数据库,但是查找出来的结果让我很纳闷,希望各位高手可以给我下解释。table 如下 :Create table test (
id Varchar(50) NOT NULL,
topic Varchar(255) NOT NULL,
class Char(10) NOT NULL,
Primary Key (hcp_id)) ENGINE = InnoDB
DEFAULT CHARACTER SET gb2312;Create Index topic_index ON test (topic);然后我有两个查询语句,分别如下:
1 select * from test where class=‘1’ and (topic like ‘%1%’ or topic like ‘%2%’ ) order by id limit ?,?2 select * from test where (topic like ‘%1%’ or topic like ‘%2%’ ) order by id limit ?,?以上两个表查出来的数据都是一样的,都是104776个。我对limit的设置是查出20个结果。按照我对数据库的理解,因为id是主键,所以mysql会自动给其加上index,而topic的index我是自己加的,所以,(2)查询时,只对 index做了查询,应该速度更快, 而(1)查询条件中的class是没有索引的,所以速度应该较慢,但是我查询的平均速度
(1) 250~800ms
(2) 300~1500ms
也就是说(1)的速度比(2)快,让我很不能理解,希望有人可以给出解释。注,本身"my.ini"我没有做任何修改和优化,只是为了测试。谢谢。