索引出错,所有查询都很慢

时间:2014-10-02 08:05:45

标签: php mysql performance

这张表有世界上所有问题:( 搜索字段为name_ar,name_en为Query LIKE, 使用字段cid,uid,sid,ssid,product_id,havproduct进行SELECT查询时速度慢 INSERT查询速度很慢 我认为这个表中的索引有错误

表格创建

CREATE TABLE `eng_subjects` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cid` int(11) NOT NULL,
  `did` int(11) NOT NULL,
  `sid` int(11) NOT NULL,
  `ssid` int(11) NOT NULL DEFAULT '0',
  `product_id` int(11) NOT NULL DEFAULT '0',
  `havproduct` int(11) NOT NULL DEFAULT '0',
  `shortcut` int(11) DEFAULT '0',
  `uid` int(11) NOT NULL,
  `ar_name` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
  `en_name` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL,
  `ar_desc` longtext COLLATE utf8_unicode_ci NOT NULL,
  `en_desc` longtext COLLATE utf8_unicode_ci NOT NULL,
  `ar_pic` varchar(500) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `en_pic` varchar(500) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
  `video` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
  `piclinknews` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
  `active_links` int(11) NOT NULL,
  `stop_smile` int(11) NOT NULL,
  `subscribe` int(11) NOT NULL,
  `subscribe_type` int(11) NOT NULL,
  `close` int(11) NOT NULL DEFAULT '0',
  `active` int(11) NOT NULL DEFAULT '1',
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `orderBy` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `viewnum` int(11) NOT NULL DEFAULT '1',
  `updated` int(11) NOT NULL DEFAULT '0',
  `pin_from` int(11) NOT NULL DEFAULT '0',
  `pin_to` int(11) NOT NULL DEFAULT '0',
  `up_to` int(11) NOT NULL DEFAULT '0',
  `deleted` int(11) NOT NULL DEFAULT '0',
  `deleted_reason` varchar(250) CHARACTER SET utf8 NOT NULL DEFAULT 'not Deleted',
  `average_price` float NOT NULL DEFAULT '0',
  `last_update` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0000-00-00 00:00:00',
  `country_id` int(11) NOT NULL,
  `discomment` int(11) NOT NULL,
  `user_active` tinyint(4) NOT NULL DEFAULT '1',
  `dep_active` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `subject_idx_did` (`did`),
  KEY `idx_uid` (`uid`),
  KEY `idx_cid` (`cid`),
  KEY `idx_pin2` (`sid`,`deleted`,`pin_to`,`close`),
  KEY `engine4_subject_date` (`date`),
  KEY `idx_cid2` (`cid`,`deleted`,`close`),
  KEY `engine4_subject1` (`product_id`,`deleted`),
  KEY `idx_prodid1` (`product_id`,`deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=22383826 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

查询插入

INSERT INTO eng_subjects (`ar_name`,`en_name`,`video`,`piclinknews`,`ar_desc`,`en_desc`,`country_id`,`active_links`,`stop_smile`,`subscribe`,`subscribe_type`,`shortcut`,`cid`,`did`,`sid`,`ssid`,`orderBy`,`discomment`,`uid`) 
VALUES ('test title','test title','','','testing content','testing content','62','','','','','','24','26','437','0','2014-07-26 11:08:40','0','1165262')

结果插入 http://i.stack.imgur.com/MEyPB.png

查询选择

select * from `eng_subjects` WHERE `cid` = 24 and active = 1 and `deleted` = 0 order by id desc LIMIT 30

解释

id  select_type table       type    possible_keys       key     key_len ref     rows    Extra
1   SIMPLE      eng_subjects ref    idx_cid,idx_cid2    idx_cid 4       const   205438  Using where

每次访问此主题时,此表都会在字段views中进行更新 我认为复制结构和创建具有相同名称的新表 并将所有数据移动到新表并删除旧表中的所有索引  但是在此表中添加索引的最佳方法是什么。


我认为添加剂的问题很慢,因为大量的索引不好的东西..删除所有索引怎么样?

  

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment eng_subjects 0 PRIMARY 1 id A 196089 NULL NULL BTREE
eng_subjects 1 subject_idx_did 1 did A 3 NULL NULL BTREE
eng_subjects 1 idx_uid 1 uid A 49022 NULL NULL BTREE
eng_subjects 1 idx_pin2 1 sid A 139 NULL NULL BTREE
eng_subjects 1 idx_pin2 2 deleted A 338 NULL NULL BTREE
eng_subjects 1 idx_pin2 3 pin_to A 1203 NULL NULL BTREE
eng_subjects 1 idx_pin2 4 close A 1474 NULL NULL BTREE
eng_subjects 1 engine4_subject_date 1 date A 196089 NULL NULL BTREE
eng_subjects 1 engine4_subject1 1 product_id A 4 NULL NULL BTREE
eng_subjects 1 engine4_subject1 2 deleted A 4 NULL NULL BTREE
eng_subjects 1 idx_prodid1 1 product_id A 4 NULL NULL BTREE
eng_subjects 1 idx_prodid1 2 deleted A 4 NULL NULL BTREE
eng_subjects 1 idx_for_qry 1 cid A 100 NULL NULL BTREE
eng_subjects 1 idx_for_qry 2 active A 100 NULL NULL BTREE
eng_subjects 1 idx_for_qry 3 deleted A 100 NULL NULL BTREE

1 个答案:

答案 0 :(得分:0)

也许尝试使用此键:

KEY `idx_for_qry` (`cid`,`active `,`deleted`)

并删除'idx_cid',因为如果你有一个复合键,那么键的第一个字段就是索引