Very very slow in the query when using IN
this query
SELECT *
FROM engine4_comment
WHERE sid IN (10,12,548,2110,5241,1255)
and this create table
CREATE TABLE `engine4_comment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sid` int(11) NOT NULL,
`rid` int(11) NOT NULL DEFAULT '0',
`uid` int(11) NOT NULL,
`text` longtext COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` int(11) NOT NULL DEFAULT '1',
`deleted` int(11) NOT NULL DEFAULT '0',
`deleted_reason` text CHARACTER SET latin1 NOT NULL,
`send_email` int(11) NOT NULL DEFAULT '1',
`user_active` tinyint(4) NOT NULL DEFAULT '1',
`dep_active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `dat_idx` (`date`),
KEY `rid_idx` (`rid`),
KEY `rsid_idx` (`rid`,`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=719329 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
and when use explaine explain SELECT * FROMengine4_commentWHERE sid IN (10,12,548,2110,5241,1255)
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE engine4_comment ALL NULL NULL NULL NULL 875583 Using where
This is your query:
SELECT *
FROM engine4_comment
WHERE sid IN (10,12,548,2110,5241,1255)
For performance, you want an index on sid. Either include a KEY statement in the create table. Or, explicitly create an index:
create index idx_engine4_comment_sid on engine4_comment(sid);
Note that the index rsid_idx doesn't help for this query, because sid is the second column. An index on (sid, rid) would benefit this query.
Related
I have another problem when i used query with INNER JOIN
this query
SELECT *
FROM `engine4_product_file` INNER JOIN
`engine4_file`
ON engine4_product_file.fid = engine4_file.id
WHERE engine4_product_file.pid IN (3347,3346,3345,3343,3342,3337) and
engine4_file.active = 1 AND
engine4_file.ext IN ('jpg','gif','png','jpeg')
and this create table engine4_product_file
CREATE TABLE `engine4_product_file` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fid` int(11) NOT NULL,
`pid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `engine4_product_file` (`fid`),
KEY `pid` (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=6549 DEFAULT CHARSET=latin1
and this create table engine4_file
CREATE TABLE `engine4_file` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`name` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`url` longtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` int(11) NOT NULL DEFAULT '1',
`size` int(11) DEFAULT NULL,
`ext` varchar(10) DEFAULT NULL,
`folder` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=48801 DEFAULT CHARSET=latin1
this explain
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE engine4_product_file range engine4_product_file,pid pid 4 NULL 30 Using where
1 SIMPLE engine4_file eq_ref PRIMARY PRIMARY 4 akafine_social2.engine4_product_file.fid 1 Using where
Change your WHERE conditions
WHERE engine4_file.active = 1 AND
engine4_file.ext IN ('jpg','gif','png','jpeg') AND
engine4_product_file.pid IN (3347,3346,3345,3343,3342,3337)
Add an index
ALTER TABLE engine4_file ADD KEY (active,ext)
I have one big problem in the slow query select
0.3054 sec
this the query
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
and when i use this
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
and this table create
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
and the table have 200000 record or more from data
You have no keys on your data. For your particularly query, the best index is:
create index id_subject_4 on subject(active, deleted, cid, id)
By the way, you should only use single quotes for string and date constants. All the values in your query are integers, so remove the quotes:
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;
First sorry, i am french and i don't speak very well english.
I have a rather strange problem with my index.
my table t_bloc (my table contains all the posts)
t_bloc
CREATE TABLE `t_bloc` (
`id_bloc` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_rubrique` int(10) unsigned NOT NULL DEFAULT '0',
`titre` varchar(100) NOT NULL DEFAULT 'A compléter',
`contenu` text NOT NULL,
`titre_page` varchar(100) NOT NULL,
`desc_courte` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`follow_url` tinyint(1) unsigned NOT NULL DEFAULT '1' ,
`image` varchar(120) NOT NULL,
`video` varchar(120) NOT NULL,
`note` tinyint(3) unsigned NOT NULL DEFAULT '0',
`champopt1` text NOT NULL,
`champopt2` text NOT NULL,
`permalien` varchar(100) NOT NULL,
`en_ligne` tinyint(1) NOT NULL DEFAULT '0',
`id_utilisateur` int(10) unsigned NOT NULL DEFAULT '1' ,
`date_crea` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modif` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`nb_commentaires` smallint(5) unsigned NOT NULL DEFAULT '0',
`nb_jaimes` int(10) unsigned NOT NULL DEFAULT '0',
`nb_jaimespas` int(10) unsigned NOT NULL DEFAULT '0',
`pourcentage_jaimes` smallint(2) unsigned NOT NULL DEFAULT '0' ,
`niveau_classement` tinyint(1) unsigned NOT NULL DEFAULT '1' ,
`id_bloc_parent` int(10) unsigned NOT NULL DEFAULT '0' ,
`id_membre_bloc` int(10) unsigned NOT NULL DEFAULT '0' ,
`valeur_pts_bloc` smallint(5) unsigned NOT NULL DEFAULT '0' ,
`commentaires_actifs` tinyint(1) unsigned NOT NULL DEFAULT '1' ,
PRIMARY KEY (`id_bloc`),
KEY `date_modif` (`date_modif`),
KEY `id_bloc_parent` (`id_bloc_parent`),
KEY `idx_rub_ligne_niv_etc` (`id_rubrique`,`en_ligne`,`niveau_classement`,`note`,`pourcentage_jaimes`,`date_modif`),
KEY `idx_tri` (`en_ligne`,`niveau_classement`,`note`,`pourcentage_jaimes`,`date_modif`),
KEY `idx_ligne_membre` (`en_ligne`,`id_membre_bloc`,`id_rubrique`),
KEY `id_membre_bloc` (`id_membre_bloc`),
KEY `idx_rub_ligne_date` (`id_rubrique`,`en_ligne`,`date_modif`,`id_bloc`),
KEY `idx_ligne_date` (`en_ligne`,`date_modif`),
FULLTEXT KEY `idx_fullindex` (`titre`,`contenu`)
) ENGINE=MyISAM AUTO_INCREMENT=456469 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
My table t_taxon_bloc
t_taxon_bloc
CREATE TABLE `t_taxon_bloc` (
`id_taxon` int(10) unsigned NOT NULL,
`id_bloc` int(10) unsigned NOT NULL,
`url_plateforme` varchar(255) NOT NULL,
`width_flash` smallint(5) unsigned NOT NULL,
`height_flash` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id_taxon`,`id_bloc`),
KEY `id_bloc` (`id_bloc`),
KEY `id_taxon` (`id_taxon`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
I have a problem when I execute this query:
select b.id_bloc FROM t_bloc as b WHERE b.en_ligne = 1 AND EXISTS ( SELECT 1 FROM t_taxon_bloc AS TB WHERE TB.id_bloc=B.id_bloc AND TB.id_taxon= 83) ORDER BY b.en_ligne DESC, b.date_modif DESC LIMIT 0, 20
I get the following explain:
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
PRIMARY
b
ref
idx_tri,idx_ligne_membre,idx_ligne_date
idx_ligne_date
1
const
58210
Using where
2
DEPENDENT SUBQUERY
TB
eq_ref
PRIMARY,id_bloc,id_taxon
PRIMARY
8
const,sitajeuxtestbourrage.b.id_bloc
1
Using index
it does not fully use the idx_ligne_date index and rows = all rows in the table
Extra = « using Where »
But if I create the following index idx_ligne_date_idbloc (en_ligne, date_modif, id_bloc) ,
the use of the index is a little better, the application runs a little faster.
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
PRIMARY
b
ref
idx_tri,idx_ligne_membre,idx_ligne_date_idbloc
idx_ligne_date_idbloc
1
const
58252
Using where; Using index
2
DEPENDENT SUBQUERY
TB
eq_ref
PRIMARY,id_bloc,id_taxon
PRIMARY
8
const,sitajeuxtestbourrage.b.id_bloc
1
Using index
Extra = « using Where, Using Index »
My questions:
id_bloc does not appear in the where and order by clauses, why am I required to add id_bloc on my multiple index ?
And Why rows = (again) all my table ? And not 20 (LIMIT 0,20)
You seem to have a misunderstanding what "Using Index" means in the Extra column. Have a look at this french explanation: http://use-the-index-luke.com/fr/sql/plans-dexecution/mysql/operations
id_bloc does not appear in the where and order by clauses, why am I required to add id_bloc on my multiple index ?
You are not required to, but doing so allows a so-called index-only scan (which is indicated by the words "Using Index" in Extra). You can learn about index-only scan at this french page: http://use-the-index-luke.com/fr/sql/regrouper-les-donnees/parcours-d-index-couvrants
Im wondering why the following query doesn't use an index when I EXPLAIN it.
EXPLAIN SELECT
`t`.`id`, `t`.`omschrijving_kort`, `t`.`oplos_tijd_issue`
FROM `incident` `t`
LEFT OUTER JOIN `plaats` `plaatsen` ON (`plaatsen`.`incident_id`=`t`.`id`)
WHERE
t.type='2' AND t.status='1'
GROUP BY t.id
ORDER BY `t`.`oplos_tijd_issue`
My table:
CREATE TABLE `incident` (
`id` int(10) unsigned NOT NULL auto_increment,
`omschrijving_kort` varchar(40) NOT NULL,
`sla_type` tinyint(1) unsigned NOT NULL,
`prioriteit` tinyint(1) unsigned NOT NULL,
`laatste_wijziging` timestamp NULL default NULL,
`aanmaak_tijd` timestamp NULL default NULL,
`start_tijd_issue` timestamp NULL default NULL,
`oplos_tijd_issue` timestamp NULL default NULL,
`impact` int(11) unsigned default NULL,
`template_id` int(10) unsigned default NULL,
`type` tinyint(1) unsigned NOT NULL default '1',
`subtype` tinyint(1) NOT NULL default '1',
`status` tinyint(1) unsigned NOT NULL default '1',
`regio_id` smallint(5) unsigned default NULL,
PRIMARY KEY (`id`),
KEY `FK_template` (`template_id`),
KEY `start_tijd_issue` (`start_tijd_issue`),
KEY `id_startTijdIssue` (`id`,`start_tijd_issue`),
KEY `id_oplosTijdIssue` (`id`,`oplos_tijd_issue`),
KEY `id_subtype_status` (`id`,`subtype`,`status`),
KEY `id` (`id`,`afsluiting_tijd`),
KEY `id_2` (`id`,`oplos_tijd_issue`,`status`),
KEY `FK_regios` (`regio_id`),
KEY `id_3` (`id`,`status`),
KEY `id_4` (`id`,`start_tijd_issue`,`oplos_tijd_issue`,`type`,`status`),
KEY `id_5` (`id`,`prioriteit`),
KEY `id_6` (`id`,`type`,`status`,`regio_id`),
KEY `id_7` (`id`,`oplos_tijd_issue`,`type`,`status`,`regio_id`),
KEY `id_8` (`id`,`regio_id`),
KEY `oplos_tijd_issue_2` (`oplos_tijd_issue`,`type`,`status`),
KEY `id_9` (`oplos_tijd_issue`,`type`,`status`,`id`),
KEY `id_10` (`id`,`type`,`status`),
KEY `type` (`type`,`status`,`id`,`oplos_tijd_issue`),
KEY `oplos_tijd_issue` (`type`,`status`,`oplos_tijd_issue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I have tried adding indexes to my table.. Probably too many!
From what I've looked up, an index with (type, status, oplos_tijd_issue) should suffice?
EXPLAIN tells me:
possible keys: type, oplos_tijd_issue
key: type
ref: const, const
Extra: Using where; Using temporary; Using filesort
I suspect the problem is a type conversion problem. This can confuse MySQL and indexes. Try this:
EXPLAIN SELECT
`t`.`id`, `t`.`omschrijving_kort`, `t`.`oplos_tijd_issue`
FROM `incident` `t`
LEFT OUTER JOIN `plaats` `plaatsen` ON (`plaatsen`.`incident_id`=`t`.`id`)
WHERE
t.type=2 AND t.status=1
GROUP BY t.id
ORDER BY `t`.`oplos_tijd_issue`
Be careful keeping numeric types separate from character types, when you can.
I have this query showing up in MySql slow query log. (It is not slow, but it is not using indexes right). I need some help on how to set up the index right.
SELECT tbladded.amount*SUM(tbladdeditem.amount)
FROM tbladded
INNER JOIN tbladdeditem ON tbladded.addedid = tbladdeditem.addedid AND tbladdeditem.deleted='False'
WHERE tbladded.userid=100
AND tbladded.date='2012-01-01'
AND tbladded.deleted='False'
GROUP BY tbladded.addedid
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tbladded ref PRIMARY,userid_date userid_date 8 const,const 1 Using where
1 SIMPLE tbladdeditem ref addedid addedid 5 tbladded.addedid 1 Using where
This is how the tables look like:
CREATE TABLE `tbladded` (
`addedid` int(11) NOT NULL AUTO_INCREMENT,
`amount` double DEFAULT NULL,
`date` date DEFAULT NULL,
`userid` mediumint(9) DEFAULT NULL,
`deleted` enum('False','True') CHARACTER SET latin1 DEFAULT 'False',
PRIMARY KEY (`addedid`),
KEY `userid_date` (`userid`,`date`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbladdeditem` (
`addeditemid` int(11) NOT NULL AUTO_INCREMENT,
`amount` double DEFAULT NULL,
`addedid` int(11) DEFAULT NULL,
`userid` mediumint(9) DEFAULT NULL,
`deleted` enum('False','True') CHARACTER SET latin1 DEFAULT 'False',
PRIMARY KEY (`addeditemid`),
KEY `addedid` (`addedid`),
KEY `userid` (`userid`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
try this:
ALTER TABLE `tbladded` ADD INDEX
`tbladdedIndex` (`userid`, `date`, `deleted`);