2014-09-28 3 views
0

나는이mysql을 선택 쿼리는 매우 느리고 filesort를 사용

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 
를 사용할 때 느린 쿼리에서 하나의 큰 문제가 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 

을 선택하고있다

및이 표 만들기

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 이상의 레코드가 있습니다.

답변

0

데이터에 키가 없습니다. 특히 쿼리의 경우 최상의 인덱스는 다음과 같습니다.

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;