Mysql doesn't use my Index correctly - mysql

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

Related

MySQL Query Slows Dramatically With Order BY included

I know this has been addressed, but the solutions have not helped me.
I have a table with 3 million records. The columns are indexed.
SELECT * FROM checklist_search WHERE player LIKE '%Mike%' AND player LIKE '%Trout%'
ORDER BY 0+year ASC, search_data ASC, 0+cardnumber ASC
LIMIT 0, 50
Without the ORDER BY the query is fast - Query took 0.0156 sec
With the ORDER BY Query took 3.7068 sec
Based on other answers to this problem I tried
SELECT * FROM(SELECT * FROM checklist_search WHERE player LIKE '%Mike%' AND player LIKE '%Trout%'
) as myalias ORDER BY 0+year ASC, search_data ASC, 0+cardnumber ASC
LIMIT 0, 50
But the results are the same.
Here is the table and index structure
CREATE TABLE IF NOT EXISTS `checklist_search` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`page_type` varchar(10) NOT NULL DEFAULT '',
`checklist_db` varchar(20) NOT NULL DEFAULT '',
`set_id` int(10) unsigned NOT NULL,
`card_id` int(11) NOT NULL,
`search_data` varchar(255) NOT NULL DEFAULT '0',
`year` varchar(10) NOT NULL DEFAULT '0',
`brand` varchar(100) NOT NULL,
`issue` varchar(100) NOT NULL,
`set_name` varchar(150) NOT NULL DEFAULT '',
`cardnumber` varchar(10) NOT NULL,
`player` varchar(150) NOT NULL DEFAULT '',
`team` varchar(150) NOT NULL DEFAULT '',
`rc` int(1) NOT NULL,
`sp` int(1) NOT NULL,
`gu` int(1) NOT NULL,
`auto` int(1) NOT NULL,
`parallel` int(1) NOT NULL,
`insert_card` int(1) NOT NULL,
`card_count` int(10) NOT NULL,
`image_count` int(10) NOT NULL,
`print_run` varchar(20) NOT NULL,
`display_image_front` varchar(120) NOT NULL,
`display_image_back` varchar(120) NOT NULL,
`seo_url` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `card_id` (`card_id`),
KEY `checklist_db` (`checklist_db`),
KEY `year` (`year`),
KEY `player` (`player`),
KEY `team` (`team`),
KEY `print_run` (`print_run`),
KEY `rc` (`rc`),
KEY `set_id` (`set_id`),
KEY `search_data` (`search_data`),
KEY `cardnumber` (`cardnumber`),
FULLTEXT KEY `player_2` (`player`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 PACK_KEYS=0 AUTO_INCREMENT=3203103 ;
Any ideas would be greatly appreciated.
Thanks
Mike
EDIT: Here is the result for
EXPLAIN SELECT *
FROM checklist_search
WHERE player LIKE '%Mike%'
AND player LIKE '%Trout%'
ORDER BY year ASC , search_data ASC , cardnumber ASC
LIMIT 0 , 50
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE checklist_search ALL NULL NULL NULL NULL 2837591 Using where; Using filesort

optimize mysql query across multiple tables

I am having some issues optimizing the following query that joins multiple tables.
SELECT count(*) as count, `snapshot_id`, `snapshot_guid`, `image`, `subject`, `name`, `brands`.`facebook`, `brands`.`brand_id`, `brand_guid`, `date_sent`
FROM (`snapshots`)
INNER JOIN `brands` ON `snapshots`.`brand_id` = `brands`.`brand_id`
WHERE `snapshots`.`status` = 1
AND `brands`.`status` = 1
AND `brands`.`archive` = 0
GROUP BY `snapshots`.`brand_id`, `snapshots`.`subject`
ORDER BY `date_sent` desc
LIMIT 20
Execution Time: 4.9 seconds
Using EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE brands ALL PRIMARY,brand_id,status,brand_status NULL NULL NULL 338 Using where; Using temporary; Using filesort
1 SIMPLE snapshots ref brand_id,status,snapshot_brand_status snapshot_brand_status 5 mockd_catalog.brands.brand_id,const 166
Describe Tables for Brands and Snapshots:
Describe brands
Field Type Null Key Default Extra
brand_id int(11) NO PRI NULL auto_increment
brand_guid char(12) NO MUL NULL
friendly varchar(128) YES NULL
name varchar(128) NO NULL
url varchar(2048) YES NULL
logo text YES NULL
cover text YES NULL
facebook varchar(2048) YES NULL
address_1 varchar(128) YES NULL
address_2 varchar(128) YES NULL
city varchar(50) YES NULL
state varchar(50) YES NULL
postal varchar(20) YES NULL
country_code varchar(128) YES NULL
date_created datetime YES NULL
date_modified datetime YES NULL
hp_snapshot tinyint(1) NO 0
status tinyint(1) YES MUL 0
archive tinyint(1) NO 0
Describe snapshots
Field Type Null Key Default Extra
snapshot_id int(11) NO PRI NULL auto_increment
snapshot_guid char(36) YES MUL NULL
brand_id int(11) NO MUL 0
email varchar(256) NO MUL NULL
seed_email varchar(256) NO NULL
date_sent datetime NO MUL NULL
date_created datetime NO NULL
date_modified datetime YES NULL
content_type varchar(10) YES NULL
subject varchar(256) NO NULL
source longtext YES NULL
html longtext NO NULL
html_error text YES NULL
thumbnail text YES NULL
image text YES NULL
status tinyint(1) NO MUL 0
archive tinyint(1) NO 0
tags text YES NULL
I've tried everything that I can think of, and can't seem to further optimize this query. Any help is appreciated.
Rick
edit 3/22/2015 # 10:27am ET:
CREATE TABLE `snapshots` (
`snapshot_id` int(11) NOT NULL AUTO_INCREMENT,
`snapshot_guid` char(36) DEFAULT NULL,
`brand_id` int(11) NOT NULL DEFAULT '0',
`email` varchar(256) NOT NULL,
`seed_email` varchar(256) NOT NULL,
`date_sent` datetime NOT NULL,
`date_created` datetime NOT NULL,
`date_modified` datetime DEFAULT NULL,
`content_type` varchar(10) DEFAULT NULL,
`subject` varchar(256) NOT NULL,
`source` longtext,
`html` longtext NOT NULL,
`html_error` text,
`thumbnail` text,
`image` text,
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '-1= error, 0 = new, 1 = approved, 2 = review ',
`archive` tinyint(1) NOT NULL DEFAULT '0',
`tags` text,
PRIMARY KEY (`snapshot_id`),
KEY `snapshot_id` (`snapshot_id`) USING BTREE,
KEY `brand_id` (`brand_id`),
KEY `email` (`email`(255)),
KEY `status` (`status`),
KEY `snapshot_guid` (`snapshot_guid`) USING BTREE,
KEY `subject` (`subject`(255)),
KEY `archive` (`archive`),
KEY `archive_status` (`archive`,`status`),
KEY `date_sent` (`date_sent`) USING BTREE,
KEY `recent_snapshots` (`snapshot_id`,`snapshot_guid`,`archive`,`status`,`brand_id`,`date_sent`,`subject`(255)) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=95002 DEFAULT CHARSET=utf8;
Brands Table:
CREATE TABLE `brands` (
`brand_id` int(11) NOT NULL AUTO_INCREMENT,
`brand_guid` char(12) NOT NULL,
`friendly` varchar(128) DEFAULT NULL,
`name` varchar(128) NOT NULL,
`url` varchar(2048) DEFAULT NULL,
`logo` text,
`cover` text,
`facebook` varchar(2048) DEFAULT NULL,
`address_1` varchar(128) DEFAULT NULL,
`address_2` varchar(128) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`state` varchar(50) DEFAULT NULL,
`postal` varchar(20) DEFAULT NULL,
`country_code` varchar(128) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`date_modified` datetime DEFAULT NULL,
`hp_snapshot` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) DEFAULT '0',
`archive` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`brand_id`),
KEY `brand_guid` (`brand_guid`) USING BTREE,
KEY `brand_id` (`brand_id`),
KEY `status` (`status`) USING BTREE,
KEY `archive_status` (`archive`,`status`),
KEY `archive` (`archive`)
) ENGINE=InnoDB AUTO_INCREMENT=423 DEFAULT CHARSET=utf8;
The select statement:
SELECT
COUNT(*) AS count,
`snapshot_id`,
`snapshot_guid`,
`image`,
`subject`,
`name`,
`brands`.`facebook`,
`brands`.`brand_id`,
`brand_guid`,
`date_sent`
FROM
(`snapshots`)
INNER JOIN
`brands` ON `snapshots`.`brand_id` = `brands`.`brand_id`
WHERE
`snapshots`.`archive` = 0
AND `snapshots`.`status` = 1
AND `brands`.`archive` = 0
AND `brands`.`status` = 1
GROUP BY `snapshots`.`brand_id` , `snapshots`.`subject`
ORDER BY `date_sent` DESC
LIMIT 20;
Explain:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE snapshots ref brand_id,status,archive,archive_status status 1 const 48304 Using where; Using temporary; Using filesort
1 SIMPLE brands eq_ref PRIMARY,brand_id,status,archive_status,archive PRIMARY 4 mockd_catalog.snapshots.brand_id 1 Using where
Ther are several problems :
The key on date_send is defined using hash while you are using it in order by date_sent.
You have a where clause with brand.status and brand.archive. archive is only in a combined key brand_id, status, archive, but it cannot be used because the first column (brand_id) is not used in the where clause. Create an index for archive only, or a composite (status, archive).
subject needs an index on its own. Currently its index is buried deep down in a composite index.
As a general rule the order of the columns in a composite indexes important. The index is only useful if the used columns are the first ones. Also they are less effective the wider the first column is. This means that
KEY `snap_indx` (`snapshot_id`,`snapshot_guid`,`email`(255),`date_sent`,`subject`(255),`status`,`archive`)
is inefficient because subject with its 255 bytes comes before status and archive which have only 4 bytes each.

verey Slow when use IN in table mysql 46.4878 sec

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.

mysql query taking about 3 seconds - and all keys indexed

I have a simple join between 3 mysql tables with 1613, 369, and 500 rows respectively. I believe I have everything indexed but the query is still taking 3 seconds or more. My query is:
SELECT
fe.pid, fe.encounter, s.Templates_ID
FROM
(form_encounter fe
JOIN em__structure s ON fe.pc_catid=s.Templates_ID
JOIN em__assemblies a ON s.Massemblies_ID=a.Massemblies_ID)
When I do an EXPLAIN on this I get the following, sorry for the format:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index Massemblies_ID Massemblies_ID 4 NULL 651 Using index; Using temporary; Using filesort
1 SIMPLE s ref Templates_ID,Massemblies_ID Massemblies_ID 4 oaks_1410.a.Massemblies_ID 1
1 SIMPLE fe ref pc_catid pc_catid,4,oaks_1410.s.Templates_ID 100 Using where
Here are my 3 tables, I am trying to put every available clue in place to figure this out:
CREATE TABLE `form_encounter` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`date` datetime DEFAULT NULL,
`reason` longtext,
`facility` longtext,
`facility_id` int(11) NOT NULL DEFAULT '0',
`pid` bigint(20) DEFAULT NULL,
`encounter` bigint(20) DEFAULT NULL,
`onset_date` datetime DEFAULT NULL,
`sensitivity` varchar(30) DEFAULT NULL,
`pc_catid` int(11) NOT NULL DEFAULT '5' COMMENT 'event category from openemr_postcalendar_categories',
`last_stmt_date` date DEFAULT NULL,
`stmt_count` int(11) NOT NULL DEFAULT '0',
`provider_id` int(11) DEFAULT '0' COMMENT 'default and main provider for this visit',
`supervisor_id` int(11) DEFAULT '0' COMMENT 'supervising provider, if any, for this visit',
`invoice_refno` varchar(31) NOT NULL DEFAULT '',
`referral_source` varchar(31) NOT NULL DEFAULT '',
`billing_facility` int(11) NOT NULL DEFAULT '0',
`billing_status` int(11) DEFAULT '10' COMMENT 'see form_encounter_status_codes',
`billing_due_date` date DEFAULT NULL COMMENT 'date an encounter becomes due',
`last_level_billed` int(11) NOT NULL DEFAULT '0' COMMENT '0=none, 1=ins1, 2=ins2, etc',
`last_level_closed` int(11) NOT NULL DEFAULT '0' COMMENT '0=none, 1=ins1, 2=ins2, etc',
`billing_note` text,
`workmans_comp` varchar(1) DEFAULT 'N',
`workmans_comp_claim_number` varchar(40) DEFAULT NULL COMMENT 'workmans comp claim number',
`prior_authorization_number` varchar(40) DEFAULT NULL COMMENT 'prior authorization number for workmans comp',
`auto_accident` varchar(1) DEFAULT 'N',
`auto_accident_state` varchar(2) DEFAULT NULL COMMENT 'auto accident state',
`other_accident` varchar(1) DEFAULT 'N',
`referral_provider_id` int(11) DEFAULT NULL,
`referral_provider_npi` varchar(10) DEFAULT NULL,
`signed_by` int(11) NOT NULL DEFAULT '0' COMMENT 'Provider who signed the encounter',
`pc_templateid` varchar(10) DEFAULT NULL COMMENT 'Physical exam template used for this encounter',
`pc_poptemplateid` int(10) unsigned DEFAULT NULL COMMENT 'Encounter template including data population',
`pc_poptemplatestatus` tinyint(1) unsigned DEFAULT NULL COMMENT 'Whether ecounter has been materially modified from template',
`deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'flag indicates form has been deleted',
PRIMARY KEY (`id`),
UNIQUE KEY `pid_encounter_unique` (`pid`,`encounter`) USING BTREE,
KEY `pc_catid` (`pc_catid`)
) ENGINE=InnoDB AUTO_INCREMENT=1614 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
Second table:
CREATE TABLE `em__structure` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Templates_ID` int(11) unsigned DEFAULT NULL,
`Sections_ID` int(11) unsigned DEFAULT NULL,
`Idx` mediumint(4) unsigned DEFAULT '1',
`Massemblies_ID` int(5) unsigned NOT NULL DEFAULT '0',
`EditDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `Templates_ID` (`Templates_ID`),
KEY `Sections_ID` (`Sections_ID`),
KEY `Massemblies_ID` (`Massemblies_ID`)
) ENGINE=InnoDB
Third table:
CREATE TABLE `em__assemblies` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Massemblies_ID` int(5) unsigned NOT NULL DEFAULT '0',
`Groupings_ID` int(11) unsigned DEFAULT NULL,
`Components_ID` int(11) unsigned DEFAULT NULL,
`Idx` mediumint(4) unsigned DEFAULT '1',
`Sex` char(1) DEFAULT NULL,
`Responsetypes_ID` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `Groupings_ID` (`Groupings_ID`),
KEY `Components_ID` (`Components_ID`),
KEY `Responsetypes_ID` (`Responsetypes_ID`),
KEY `Massemblies_ID` (`Massemblies_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=505 DEFAULT CHARSET=latin1
The joins are all on indexed integer keys.

MySQL where+group+order; filesort and not using index

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.