I've tried doing some searching, but I am having a bit of trouble getting a grasp on this.
Once I explain a query, how can I look at the information and determine the best place to add an index to speed up the query.
For example I have this query:
SELECT a.app_id, DATE_FORMAT(app_datetime, '%c/%d/%y %H:%i') as app_datetime, app_language
, if((select count(w.app_id) from li_app_cnc as w where w.app_id = a.app_id) > 0 , concat('CNC: ',(select cnc_note from li_app_cnc as w where w.app_id = a.app_id)),
if((select count(x.app_id) from li_app_dnc as x where x.app_id = a.app_id) > 0, concat('DNC: ', (select cancel_note from li_app_dnc as x where x.app_id = a.app_id)),
if((select count(y.app_id) from li_app_canceled as y where y.app_id = a.app_id) > 0, concat('Canceled: ', (select cancel_note from li_app_canceled as y where y.app_id = a.app_id)),
concat(h.emp_firstname, ' ', h.emp_lastname)))) as int_id, app_notes, app_facility, app_department
, app_requesting_person, app_service_provider
, cast(AES_DECRYPT(les_name, '$privatekey') as char) as les_name
, les_dob, cast(AES_DECRYPT(les_medicaid_id, '$privatekey') as char) as les_medicaid_id
, billing_total_time, billing_workorder_received, billing_admin_fee
, billing_notes, created_by, created_on, modified_by, modified_on, wo_entered_by
, t.cancel_code, t.cancel_note
, u.cnc_code, u.cnc_note
, v.cancel_code as dnc_code, v.cancel_note as dnc_note
FROM li_appointments.li_appointments as a
left Join orangehrm_li.hs_hr_employee as h on a.terp_id = h.employee_id
left Join li_appointments.li_app_canceled as t on t.app_id = a.app_id
left Join li_appointments.li_app_cnc as u on u.app_id = a.app_id
left Join li_appointments.li_app_dnc as v on v.app_id = a.app_id
where (app_client_id in (select account_number from li_appointments.li_client_access
where id = $userid) or created_by = '$username')
and date(app_datetime) = date(now())
and a.app_id not in (select f.app_id from li_app_dnc as f)
Which takes about 14 secconds
It is explained as:
1 PRIMARY a ALL 37539 Using where
1 PRIMARY h ALL 1036
1 PRIMARY t eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
1 PRIMARY u eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
1 PRIMARY v eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
9 DEPENDENT SUBQUERY f unique_subquery PRIMARY PRIMARY 4 func 1 Using index
8 DEPENDENT SUBQUERY li_client_access ALL 72 Using where
7 DEPENDENT SUBQUERY y eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
6 DEPENDENT SUBQUERY y eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1 Using index
5 DEPENDENT SUBQUERY x eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
4 DEPENDENT SUBQUERY x eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1 Using index
3 DEPENDENT SUBQUERY w eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1
2 DEPENDENT SUBQUERY w eq_ref PRIMARY PRIMARY 4 li_appointments.a.app_id 1 Using index
Some thumb rules:
Index all the fields on which you will define WHERE conditions.
Index all the fields on which you will define relationships.
Index all the fields on which you will define grouping criteria.
Avoid indexing everything in your table. Think before creating the indexes.
(Although it depends on your specific needs, I personally avoid creating indexes on floating point columns)
Obviously, the above rules imply that you must index all the fields that work as primary key or as foreign key (if your tables are normalized, you already must have created the appropriate primary keys; if your tables are not normalized, then normalize them).
Related
Any ideas why index is not using on table SD and how to fix it?
I tried to remove the group and sort clauses but still same issue and cant find that is the problem
P.S. dont read this system wont let me post because code is more than description
Query
SELECT sd.filter_group_id, fgd.name AS group_name, sdc.filter_id AS filter_id, fd.name,
COUNT(DISTINCT p2c.product_id) AS total, f.sort_order, sd.sort_order AS sort,
(CASE
WHEN fgd.custom_order = 1
THEN COUNT(p2c.product_id)
END) AS custom_order
FROM oc_sd_filter sd
JOIN oc_product_to_category p2c ON p2c.category_id = sd.category_id
JOIN oc_product_filter sdc18 ON sdc18.product_id = p2c.product_id
JOIN oc_product_filter sdc21 ON sdc21.product_id = p2c.product_id
JOIN oc_product p ON p.product_id = p2c.product_id
JOIN oc_product_filter sdc ON sdc.product_id = p2c.product_id
JOIN oc_filter f ON sdc.filter_id = f.filter_id
JOIN oc_filter_description fd ON sdc.filter_id = fd.filter_id
JOIN oc_filter_group_description fgd ON fd.filter_group_id = fgd.filter_group_id
WHERE sd.category_id = '93'
AND p.status = '1'
AND sd.filter_group_id = fd.filter_group_id
AND sd.status = 1
AND sdc18.filter_id IN (199,200,120,321,611,451,380,542)
AND sdc21.filter_id IN (241,242)
GROUP BY fd.filter_id, fd.filter_group_id
ORDER BY sd.sort_order ASC,
(CASE
WHEN fgd.custom_order = 0
THEN f.sort_order
END) ASC,
(CASE
WHEN fgd.custom_order = 1
THEN COUNT(p2c.product_id)
END) DESC
EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE sd ALL filter,cat,status NULL NULL NULL 11 Using where; Using temporary; Using filesort
1 SIMPLE fgd ref PRIMARY,filter_group_id PRIMARY 4 example_db.sd.filter_group_id 1
1 SIMPLE p2c ref PRIMARY,category_id, category_id 4 example_db.sd.category_id 59 Using index
1 SIMPLE p eq_ref PRIMARY,status,product_id PRIMARY 4 example_db.p2c.product_id 1 Using where
1 SIMPLE sdc ref PRIMARY PRIMARY 4 example_db.p2c.product_id 9 Using index
1 SIMPLE fd ref PRIMARY,filter PRIMARY 4 example_db.sdc.filter_id 1 Using where
1 SIMPLE f eq_ref PRIMARY PRIMARY 4 example_db.sdc.filter_id 1
1 SIMPLE sdc21 ref PRIMARY PRIMARY 4 example_db.p2c.product_id 9 Using where; Using index
1 SIMPLE sdc18 ref PRIMARY PRIMARY 4 example_db.p2c.product_id 9 Using where; Using index
Table
CREATE TABLE `oc_sd_filter` (
`id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`filter_group_id` int(11) NOT NULL,
`status` int(11) NOT NULL,
`sort_order` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for table `oc_sd_filter`
--
ALTER TABLE `oc_sd_filter`
ADD PRIMARY KEY (`id`),
ADD KEY `filter` (`filter_group_id`),
ADD KEY `cat` (`category_id`),
ADD KEY `status` (`status`),
ADD KEY `sort_order` (`sort_order`);
Suggested composite indexes:
sd: INDEX(category_id, status, filter_group_id, sort_order)
fgd: INDEX(filter_group_id, name, custom_order)
sdc: INDEX(product_id, filter_id)
fd: INDEX(filter_group_id, filter_id, name)
p2c: INDEX(category_id, product_id)
f: INDEX(filter_id, sort_order)
oc_product_filter: INDEX(product_id, filter_id)
p: INDEX(status, product_id)
When adding a composite index, DROP index(es) with the same leading columns.
That is, when you have both INDEX(a) and INDEX(a,b), toss the former.
If that does not help enough, come back and we can talk about turning the query inside out -- so that the GROUP BY is done before most of the JOINs. But first, how many rows in the resultset? How many rows if you take out the GROUP BY clause?
Example (from Comment):
SELECT filter_group_id
FROM sd
WHERE status = 1
ORDER BY sort_order
The Optimal index is both composite and "covering"; the order is important:
INDEX(status, sort_order, filter_group_id)
Any longer index starting with those is essentially "as good". Any shorter index (eg, INDEX(status, sort_order) or starting with that) will be "good", but "not as good".
In particular, the 4-column index I provided above is not useful. It is OK to add both indexes; the Optimizer will decide which index to use for each SELECT.
I have a problem with performance with MySQL. How can i improve it?
The situation is following:
Table “backlogsap„ have about 4 mio entries.
Indexes are created
This table have FK and other tables have FK to this table => can’t
create partitions.
This query need about 140 seconds to complete:
select
idmaterial,
materialgroup,
materialgroupcategory,
name,
dispatchgroup,
idsupplier,
group_concat(distinct sellingorganizationname) as sellingorganizationnames,
group_concat(distinct idordertype) as idordertypes,
group_concat(distinct idpositiontype) as idpositiontypes,
sum(newOrUpdated and isCritical) as classA,
sum(newOrUpdated and not isCritical) as classB,
sum(processingstate <3) as classC,
(select count(innerBacklogsAp.idmaterial)
from backlogsap as innerBacklogsAp
where innerBacklogsAp.idmaterial = src.idmaterial and IsDeleted = 0) as countReplacementVehiclerRequests
from
(select
backlogsap.idmaterial as idmaterial,
backlog.processingstate as processingstate,
material.idsupplier as idsupplier,
backlogsap.sellingorganizationname as sellingorganizationname,
backlogsap.idpositiontype as idpositiontype,
backlogsap.idordertype as idordertype,
materialindistributioncenter.dispatchgroup as dispatchgroup,
material.name as name,
material.idmaterialgroup as materialgroup,
materialgroup.idmaterialgroupcategory as materialgroupcategory,
(processingstate = 0 or processingstate = 1) as newOrUpdated,
((cancellation.state is not null and cancellation.state = 0 ) or
(reminderrequest.state is not null and (reminderrequest.state = 2 or reminderrequest.state = 0))
) as isCritical
from backlogsap
join backlog using (idbacklogsap)
left join cancellation using (idcancellation)
left join reminderrequest on backlog.IdReminderRequest = reminderrequest.idreminder
left join material using (idmaterial)
left join materialindistributioncenter using (idmaterial, iddistributioncenter)
left join materialgroup using (idmaterialgroup)
where (idcancellation is null or cancellation.State not in (1)) and
backlogsap.isdeleted = 0 and
backlogsap.idordertype not in ('ZAP', 'ZAK', 'ZAKO', 'ZAKZ', 'ZAPM') and
iddistributioncenter = 1469990
) as src
group by idmaterial
order by classA desc, classB desc, classC, idmaterial desc
Explain
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived3> ALL 26960 Using temporary; Using filesort
3 DERIVED backlogsap index_merge PRIMARY,fk_BacklogSap_OrderType1_idx,
fk_BacklogSap_MaterialInDistributionCenter1_idx,
perform_backlogsap_isdeleted,
fk_BacklogSap_DistributionCenter_idx perform_backlogsap_isdeleted,fk_BacklogSap_DistributionCenter_idx 1,4 35946 Using intersect(perform_backlogsap_isdeleted,fk_BacklogSap_DistributionCenter_idx); Using where
3 DERIVED backlog eq_ref idBacklogSAP_UNIQUE,
fk_Backlog_BacklogSap1_idx,
fk_Backlog_Cancellation1_idx idBacklogSAP_UNIQUE 4 ...backlogsap.IdBacklogSap 1
3 DERIVED cancellation eq_ref PRIMARY PRIMARY 4 ...backlog.IdCancellation 1 Using where
3 DERIVED reminderrequest eq_ref PRIMARY PRIMARY 4 ...backlog.IdReminderRequest 1
3 DERIVED material eq_ref PRIMARY PRIMARY 45 ...backlogsap.IdMaterial 1
3 DERIVED materialindistributioncenter eq_ref PRIMARY,
unqiue_IdDistributionCenter_IdMaterial,
fk_MaterialDistributionCenter_DistributionCenter1_idx,
fk_MaterialDistributionCenter_Material1_idx PRIMARY 49 const,...backlogsap.IdMaterial 1
3 DERIVED materialgroup eq_ref PRIMARY PRIMARY 137 ....material.IdMaterialGroup 1
2 DEPENDENT SUBQUERY innerBacklogsAp ref perform_backlogsap_isdeleted,
idx_backlogsap_IdMaterial idx_backlogsap_IdMaterial 45 func 8 Using where
Solved: created combined Index (idmaterial, IsDeleted)
If I execute the following query
select * from medienkatalog_nct.sphinx_index limit 380000, 10000
the query hang forever between status "none" and "closing tables".
explain select * from medienkatalog_nct.sphinx_index;
# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1 SIMPLE katalognummer index PRIMARY,artikelNr,katalognr,idvideothek artikelNr 4 275870 Using index
1 SIMPLE artikel ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.katalogNr 1
1 SIMPLE artikel eq_ref PRIMARY,id_artikelnr PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE relation_kategorie ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE kategorie eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_kategorie.id_kategorie 1
1 SIMPLE relation_inhalt ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE inhalt eq_ref PRIMARY,id_inhalt PRIMARY 4 medienkatalog_nct.relation_inhalt.id_inhalt 1
1 SIMPLE relation_medium ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1 Using index
1 SIMPLE medium eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_medium.id_medium 1
1 SIMPLE relation_plattform ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE plattform eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.relation_plattform.id_plattform 1
1 SIMPLE relation_hersteller ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE hersteller eq_ref PRIMARY,id_hersteller PRIMARY 4 medienkatalog_nct.relation_hersteller.id_hersteller 1
1 SIMPLE relation_titel eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE titel eq_ref id_titel id_titel 4 medienkatalog_nct.relation_titel.id_titel 1 Using index
1 SIMPLE relation_freigabe ref PRIMARY PRIMARY 4 medienkatalog_nct.artikel.artikelNr 1 Using index
1 SIMPLE freigabe eq_ref PRIMARY,id_freigabe PRIMARY 4 medienkatalog_nct.relation_freigabe.id_freigabe 1
1 SIMPLE media eq_ref PRIMARY PRIMARY 4 medienkatalog_nct.katalognummer.artikelNr 1
1 SIMPLE artikel_videothek ref PRIMARY,i_katalognr,i_id_videothek PRIMARY 4 medienkatalog_nct.katalognummer.katalogNr 5 Using where
1 SIMPLE videothek eq_ref PRIMARY,INDEX PRIMARY 4 medienkatalog_nct.artikel_videothek.id_videothek 1 Using where
Edit: the SQL query
CREATE
ALGORITHM = UNDEFINED
DEFINER = `dbadmin`#`%`
SQL SECURITY DEFINER
VIEW `sphinx_index` AS
SELECT
((`medienkatalog_nct`.`artikel_videothek`.`id_videothek` * 1000000000) + `medienkatalog_nct`.`artikel`.`artikelNr`) AS `pkey`,
`medienkatalog_nct`.`fkey`(`medienkatalog_nct`.`artikel`.`artikelNr`,
`medienkatalog_nct`.`media`.`key_media`) AS `fkey`,
`medienkatalog_nct`.`artikel_videothek`.`id_videothek` AS `id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr` AS `artikelnr`,
`medienkatalog_nct`.`artikel_videothek`.`katalognr` AS `katalognr`,
`medienkatalog_nct`.`media`.`key_media` AS `key_media`,
`medienkatalog_nct`.`media`.`has_werbebanner` AS `has_werbebanner`,
`medienkatalog_nct`.`media`.`has_mailbanner` AS `has_mailbanner`,
`medienkatalog_nct`.`media`.`has_animation` AS `has_animation`,
`medienkatalog_nct`.`media`.`has_klappe` AS `has_klappe`,
`medienkatalog_nct`.`media`.`has_klappeemo` AS `has_klappeemo`,
`medienkatalog_nct`.`media`.`has_podcast` AS `has_podcast`,
IF(ISNULL(`medienkatalog_nct`.`media`.`top20rang`),
100,
IF((`medienkatalog_nct`.`media`.`top20rang` = 0),
50,
`medienkatalog_nct`.`media`.`top20rang`)) AS `top20rang`,
`medienkatalog_nct`.`media`.`has_trailer` AS `has_trailer`,
`medienkatalog_nct`.`media`.`has_traileremo` AS `has_traileremo`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`media`.`verleihstart`) AS `verleihstart`,
`medienkatalog_nct`.`media`.`has_vorschaubanner` AS `has_vorschaubanner`,
TOP_PREIS(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel_videothek`.`katalognr`) AS `is_toppreis`,
IF(((`nachbestellsystem`.`artikel`.`aktiv` = 1)
AND (`nachbestellsystem`.`artikel`.`lieferbar` = 1)
AND ISNULL(`nachbestellsystem`.`artikel`.`dekommissionierung`)),
1,
0) AS `is_kommission`,
`medienkatalog_nct`.`kategorie`.`beschreibung` AS `kategorie`,
`medienkatalog_nct`.`titel`.`titel` AS `titel`,
`medienkatalog_nct`.`titel`.`titel` AS `titel_sort`,
`medienkatalog_nct`.`str_to_ord`(`medienkatalog_nct`.`titel`.`titel`) AS `titel_ord`,
`medienkatalog_nct`.`titel`.`untertitel` AS `untertitel`,
`medienkatalog_nct`.`medium`.`id_medium` AS `id_medium`,
`medienkatalog_nct`.`medium`.`beschreibung` AS `medium`,
`medienkatalog_nct`.`plattform`.`id_plattform` AS `id_plattform`,
`medienkatalog_nct`.`plattform`.`beschreibung` AS `plattform`,
ID_VERSIONS(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `vkey`,
`medienkatalog_nct`.`version`(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `version`,
ID_GENRES(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `gkey`,
GENRE(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `genre`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`artikel`.`artikelstart`) AS `artikelstart`,
UNIX_TIMESTAMP(`medienkatalog_nct`.`artikel_videothek`.`erfassungsdatum`) AS `erfassungsdatum`,
`medienkatalog_nct`.`hersteller`.`id_hersteller` AS `id_hersteller`,
`medienkatalog_nct`.`hersteller`.`beschreibung` AS `hersteller`,
ACTOR(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `darsteller`,
COUNTRY(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `land`,
DIRECTOR(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `regie`,
FORMAT(RANKING(`medienkatalog_nct`.`artikel`.`artikelNr`),
2) AS `sterne`,
COUNT_RANKING(`medienkatalog_nct`.`artikel`.`artikelNr`) AS `bewertungen`,
ANZAHL_VL(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr`,
(NOW() - INTERVAL 1 MONTH),
NOW()) AS `anzahl_vl`,
ANZAHL_VK(`medienkatalog_nct`.`artikel_videothek`.`id_videothek`,
`medienkatalog_nct`.`artikel`.`artikelNr`,
(NOW() - INTERVAL 1 MONTH),
NOW()) AS `anzahl_vk`,
`medienkatalog_nct`.`inhalt`.`beschreibung` AS `inhalt`,
`medienkatalog_nct`.`inhalt`.`aufmacher` AS `aufmacher`,
`medienkatalog_nct`.`relation_kategorie`.`id_kategorie` AS `id_kategorie`,
`medienkatalog_nct`.`relation_titel`.`id_titel` AS `id_titel`,
`medienkatalog_nct`.`relation_freigabe`.`id_freigabe` AS `id_freigabe`,
`medienkatalog_nct`.`freigabe`.`beschreibung` AS `freigabe`,
CONCAT(IF((`medienkatalog_nct`.`artikel_videothek`.`ist_verleih` <> 0),
'Leihartikel ',
''),
IF((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` <> 0),
'Gebrauchtartikel Verkaufsartikel ',
''),
IF(((`medienkatalog_nct`.`artikel_videothek`.`ist_verkauf` <> 0)
AND (`medienkatalog_nct`.`artikel_videothek`.`bestand_vk` > 0)),
IF((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` = 0),
'Verkaufsartikel Neuware',
'Neuware'),
'')) AS `artikelart`,
`medienkatalog_nct`.`artikel_videothek`.`deleted` AS `deleted`
FROM
(((((((((((((((((((`medienkatalog_nct`.`artikel`
JOIN `medienkatalog_nct`.`katalognummer` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`katalognummer`.`artikelNr`)))
JOIN `medienkatalog_nct`.`artikel_videothek` ON (((`medienkatalog_nct`.`katalognummer`.`katalogNr` = `medienkatalog_nct`.`artikel_videothek`.`katalognr`)
AND (`medienkatalog_nct`.`katalognummer`.`vtid` IN (0 , `medienkatalog_nct`.`artikel_videothek`.`id_videothek`)))))
LEFT JOIN `medienkatalog_nct`.`relation_kategorie` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_kategorie`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`kategorie` ON ((`medienkatalog_nct`.`relation_kategorie`.`id_kategorie` = `medienkatalog_nct`.`kategorie`.`id_kategorie`)))
LEFT JOIN `medienkatalog_nct`.`relation_inhalt` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_inhalt`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`inhalt` ON ((`medienkatalog_nct`.`relation_inhalt`.`id_inhalt` = `medienkatalog_nct`.`inhalt`.`id_inhalt`)))
LEFT JOIN `medienkatalog_nct`.`relation_medium` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_medium`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`medium` ON ((`medienkatalog_nct`.`relation_medium`.`id_medium` = `medienkatalog_nct`.`medium`.`id_medium`)))
LEFT JOIN `medienkatalog_nct`.`relation_plattform` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_plattform`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`plattform` ON ((`medienkatalog_nct`.`relation_plattform`.`id_plattform` = `medienkatalog_nct`.`plattform`.`id_plattform`)))
LEFT JOIN `medienkatalog_nct`.`relation_hersteller` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_hersteller`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`hersteller` ON ((`medienkatalog_nct`.`relation_hersteller`.`id_hersteller` = `medienkatalog_nct`.`hersteller`.`id_hersteller`)))
LEFT JOIN `medienkatalog_nct`.`relation_titel` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_titel`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`titel` ON ((`medienkatalog_nct`.`relation_titel`.`id_titel` = `medienkatalog_nct`.`titel`.`id_titel`)))
LEFT JOIN `medienkatalog_nct`.`relation_freigabe` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`relation_freigabe`.`artikelNr`)))
LEFT JOIN `medienkatalog_nct`.`freigabe` ON ((`medienkatalog_nct`.`freigabe`.`id_freigabe` = `medienkatalog_nct`.`relation_freigabe`.`id_freigabe`)))
LEFT JOIN `medienkatalog_nct`.`media` ON ((`medienkatalog_nct`.`artikel`.`artikelNr` = `medienkatalog_nct`.`media`.`artikelnr`)))
JOIN `videotaxi`.`videothek` ON ((`medienkatalog_nct`.`artikel_videothek`.`id_videothek` = `videotaxi`.`videothek`.`id_videothek`)))
LEFT JOIN `nachbestellsystem`.`artikel` ON ((`nachbestellsystem`.`artikel`.`katalogNr` = `medienkatalog_nct`.`katalognummer`.`katalogNr`)))
WHERE
(((`medienkatalog_nct`.`artikel_videothek`.`ist_gebraucht` <> 0)
OR (`medienkatalog_nct`.`artikel_videothek`.`ist_verleih` <> 0)
OR (`medienkatalog_nct`.`artikel_videothek`.`ist_verkauf` <> 0))
AND (`videotaxi`.`videothek`.`offen` <> 0)
AND (`medienkatalog_nct`.`artikel_videothek`.`deleted` = 0))
The query worked well for more than 3 years until last week.
Does anybody have ideas?
following mysql query taking more time to fetch results so i need help to optimise
SELECT status,
count(status) AS COUNT,
build_id,
results_bu.name,
results_bu.creation_ts,
results_bu.release_date
FROM
(SELECT NHTC.parent_id AS tsuite_id,
NHTC.id AS tcase_id,
NHTC.name AS name,
TPTCV.tcversion_id,
TPTCV.platform_id,
E.build_id,
E.tcversion_number AS VERSION,
TCV.tc_external_id AS external_id,
E.id AS executions_id,
E.status AS status,
(TPTCV.urgency * TCV.importance) AS urg_imp
FROM testplan_tcversions TPTCV
/* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
JOIN
(SELECT EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id,
MAX(EE.id) AS id
FROM executions EE
WHERE EE.testplan_id=13954
AND EE.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
GROUP BY EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
AND LEBBP.platform_id = TPTCV.platform_id
AND LEBBP.tcversion_id = TPTCV.tcversion_id
AND LEBBP.testplan_id = 13954
/* Get execution status WRITTEN on DB */
JOIN executions E ON E.id = LEBBP.id
AND E.build_id = LEBBP.build_id
/* Get Test Case info from Test Case Version */
JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
/* Get Test Suite info from Test Case */
JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
/* Get Test Case Version attributes */
JOIN tcversions TCV ON TCV.id = E.tcversion_id
WHERE TPTCV.testplan_id=13954
AND E.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
UNION ALL
/* sqlUnion Test suites - not run */
SELECT NHTC.parent_id AS tsuite_id,
NHTC.id AS tcase_id,
NHTC.name AS name,
TPTCV.tcversion_id,
TPTCV.platform_id,
BU.id AS build_id,
TCV.VERSION,
TCV.tc_external_id AS external_id,
COALESCE(E.id,-1) AS executions_id,
COALESCE(E.status,'n') AS status,
(TPTCV.urgency * TCV.importance) AS urg_imp
FROM testplan_tcversions TPTCV
/* Needed to be able to put a value on build_id on output set */
JOIN builds BU ON BU.id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
/* GO FOR Absolute LATEST exec ID On BUILD,PLATFORM */
LEFT OUTER JOIN
(SELECT EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id,
MAX(EE.id) AS id
FROM executions EE
WHERE EE.testplan_id=13954
AND EE.build_id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
GROUP BY EE.tcversion_id,
EE.testplan_id,
EE.platform_id,
EE.build_id) AS LEBBP ON LEBBP.testplan_id = TPTCV.testplan_id
AND LEBBP.platform_id = TPTCV.platform_id
AND LEBBP.tcversion_id = TPTCV.tcversion_id
AND LEBBP.build_id = BU.id
AND LEBBP.testplan_id = 13954
/* Get execution status WRITTEN on DB */
LEFT OUTER JOIN executions E ON E.build_id = LEBBP.build_id
AND E.testplan_id = TPTCV.testplan_id
AND E.platform_id = TPTCV.platform_id
AND E.tcversion_id = TPTCV.tcversion_id
/* Get Test Case info from Test Case Version */
JOIN nodes_hierarchy NHTCV ON NHTCV.id = TPTCV.tcversion_id
/* Get Test Suite info from Test Case */
JOIN nodes_hierarchy NHTC ON NHTC.id = NHTCV.parent_id
/* Get Test Case Version attributes */
JOIN tcversions TCV ON TCV.id = TPTCV.tcversion_id
WHERE TPTCV.testplan_id=13954
AND BU.id IN
(SELECT id
FROM builds
WHERE testplan_id = 13954
AND active=1
AND (creation_ts BETWEEN DATE('2015-01-01') AND DATE('2015-05-10'))
ORDER BY name ASC)
/* Get REALLY NOT RUN => BOTH LEBBP.id AND E.id NULL */
AND E.id IS NULL
AND LEBBP.id IS NULL) AS RESULT
LEFT OUTER JOIN builds AS results_bu ON results_bu.id=RESULT.build_id
GROUP BY status,
build_id;
results for EXPLAIN as follows
id select_type table type possible_keys key key_len ref rows Extra
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 PRIMARY <derived2> ALL 32768 Using temporary; Using filesort
1 PRIMARY results_bu eq_ref PRIMARY PRIMARY 4 result.build_id 1
2 DERIVED <derived3> ALL 32229 Using where
2 DERIVED NHTCV eq_ref PRIMARY,pid_m_nodeorder PRIMARY 4 LEBBP.tcversion_id 1
2 DERIVED NHTC eq_ref PRIMARY PRIMARY 4 bitnami_testlink.NHTCV.parent_id 1
2 DERIVED TPTCV eq_ref testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 12 LEBBP.tcversion_id,LEBBP.platform_id 1
2 DERIVED E eq_ref PRIMARY PRIMARY 4 LEBBP.id 1 Using where
2 DERIVED TCV eq_ref PRIMARY PRIMARY 4 bitnami_testlink.E.tcversion_id 1
4 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
3 DERIVED EE ref executions_idx1 executions_idx1 4 135350 Using where; Using index
3 DERIVED BB eq_ref PRIMARY,name,testplan_id PRIMARY 4 bitnami_testlink.EE.build_id 1 Using where
5 UNION BU index testplan_id 4 569 Using where; Using index
5 UNION TPTCV ref testplan_tcversions_tplan_tcversion testplan_tcversions_tplan_tcversion 4 1472
5 UNION TCV eq_ref PRIMARY PRIMARY 4 bitnami_testlink.TPTCV.tcversion_id 1
5 UNION NHTCV eq_ref PRIMARY,pid_m_nodeorder PRIMARY 4 bitnami_testlink.TPTCV.tcversion_id 1
5 UNION NHTC eq_ref PRIMARY PRIMARY 4 bitnami_testlink.NHTCV.parent_id 1
5 UNION <derived6> ALL 32229 Using where
5 UNION E ref executions_idx1 executions_idx1 16 bitnami_testlink.TPTCV.tcversion_id,
bitnami_testlink.TPTCV.platform_id,
LEBBP.build_id 1 Using where; Not exists
8 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
7 DEPENDENT
SUBQUERY builds unique_subquery PRIMARY,name,testplan_id PRIMARY 4 func 1 Using where
6 DERIVED EE ref executions_idx1 executions_idx1 4 135350 Using where; Using index
6 DERIVED BB eq_ref PRIMARY,name,testplan_id PRIMARY 4 bitnami_testlink.EE.build_id 1 Using where
UNION RESULT <union2,5> ALL
Actually we are trying to get how many number of execution records passed, failed between period of time for selected Test plan from test link database
Thanks,
Ram
Our search is very slow on view. we can't define index on view.. Please help how we can improve this .. Below Query took 33.3993 sec.
SELECT
`v_cat_pro`.`product_id`, `v_cat_pro`.`msrp`,
FROM
`v_prod_cat` AS `v_cat_pro`
WHERE
(product_status="1" and msrp >0 AND (search_text = 'de') )
ORDER BY
`msrp` ASC LIMIT 50
Explain query result
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE cat_product index catproducts_product_id,category_product_index category_product_index 8 NULL 941343 Using index; Using temporary; Using filesort
1 SIMPLE dept eq_ref PRIMARY PRIMARY 4 newdhf.cat_product.category_id 1 Using where
1 SIMPLE team eq_ref PRIMARY PRIMARY 4 newdhf.dept.parent_id 1 Using where
1 SIMPLE league eq_ref PRIMARY PRIMARY 4 newdhf.team.parent_id 1 Using where
1 SIMPLE product eq_ref PRIMARY PRIMARY 4 newdhf.cat_product.product_id 1 Using where
CREATE
ALGORITHM=UNDEFINED VIEW v_prod_cat AS
select
dept.id AS dept_id,team.short_name AS shortteamname,team.url AS team_url,team.id AS team_id,league.id AS league_id,product.product_id AS product_id,product.product_status AS product_status,product.upload_image_l AS upload_image_l,dept.name AS department,team.name AS team,league.name AS league,product.title AS title,cat_product.product_url AS product_url,product.discount AS discount,product.discount_start_date AS discount_start_date,product.discount_end_date AS discount_end_date,product.extra_discount AS extra_discount,product.extra_discount_start_date AS extra_discount_start_date,product.extra_discount_end_date AS extra_discount_end_date,product.global_alt_tag AS global_alt_tag,product.msrp AS msrp,product.cost AS cost,product.vendor_id AS vendor_id,if((cat_product.is_default > 0),1,0) AS is_default,
concat(league.name,_utf8' ',team.name,_utf8' ',dept.name,_utf8' ',replace(replace(replace(replace(product.title,'$leaguename',league.name),'$teamname',team.name),'$shortteamname',team.short_name),'$departmentname',dept.name),' ',product.sku_code,_utf8' ',replace(replace(replace(replace(product.site_search_keyword,'$leaguename',league.name),'$teamname',team.name),'$shortteamname',team.short_name),'$departmentname',dept.name)) AS search_text
from
((((categories dept join categories team on(((team.id = dept.parent_id) and (team.category_type = _utf8'team')))) join categories league on(((league.id = team.parent_id) and (league.category_type = _utf8'league')))) join category_products cat_product on((cat_product.category_id = dept.id))) join products product on((product.product_id = cat_product.product_id))) where (dept.category_type = _utf8'department')
order by
dept.id desc;
If you add an index to your 'v_prod_cat' table on the columns that you are searching on then this should help speed up your view.