繁体   English   中英

MySQL的选择查询是非常缓慢的,并使用文件排序

[英]mysql select query is very slow and uses filesort

我在慢速查询中有一个大问题选择0.3054秒

这个查询

SELECT id, ar_name, en_name,product_id,havproduct, viewnum, uid,pin_to, sid, ssid,cid, close,date
FROM subject
where active = '1' and deleted = '0' and cid= '24'
order by id DESC
LIMIT 0,30

当我用这个

explain

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  subject     ALL     NULL    NULL    NULL    NULL    230026  Using where; Using filesort

这个表创建

CREATE TABLE `subject` (
  `id` int(11) NOT NULL,
  `cid` int(11) NOT NULL,
  `did` int(11) NOT NULL,
  `sid` int(11) NOT NULL,
  `ssid` int(11) NOT NULL,
  `product_id` int(11) NOT NULL DEFAULT '0',
  `havproduct` int(11) NOT NULL DEFAULT '0',
  `uid` int(11) NOT NULL,
  `ar_name` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `en_name` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `close` int(11) NOT NULL DEFAULT '0',
  `active` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `viewnum` int(11) NOT NULL DEFAULT '1',
  `pin_to` int(11) NOT NULL DEFAULT '0',
  `deleted` int(11) NOT NULL,
  `user_active` int(11) NOT NULL DEFAULT '1',
  `dep_active` int(11) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=utf8

并且表中有200000条记录或更多数据记录

您的数据没有密钥。 对于您特别的查询,最佳索引是:

create index id_subject_4 on subject(active, deleted, cid, id)

顺便说一句,您应该只对字符串和日期常量使用单引号。 查询中的所有值都是整数,因此请删除引号:

SELECT id, ar_name, en_name,product_id,havproduct, viewnum, uid, pin_to, sid, ssid,cid, close,date
FROM subject
where active = 1 and deleted = 0 and cid = 24
order by id DESC
LIMIT 0, 30;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM