In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `#TEMPTABLE1` ADD INDEX `temptable1_idx_fld_storagegroupid` (`fld_StorageGroupID`);
ALTER TABLE `#TEMPTABLE2` ADD INDEX `temptable2_idx_fld_storagegroupid` (`fld_StorageGroupID`);
SELECT
SUM(CYJEWELRY) 'CY_Jewelry',
SUM(CYAPPLICANCE) 'CY_Appliance',
SUM(CYCELLPHONE) 'CY_Cellphone',
SUM(PYJEWELRY) 'PY_Jewelry',
SUM(PYAPPLIANCE) 'PY_Appliance',
SUM(PYCELLPHONE) 'PY_Cellphone'
FROM
(SELECT
COUNT(*) AS CYJEWELRY,
0 AS CYAPPLICANCE,
0 AS CYCELLPHONE,
0 AS PYJEWELRY,
0 AS PYAPPLIANCE,
0 AS PYCELLPHONE
FROM
#TEMPTABLE1
WHERE
(
#TEMPTABLE1.fld_StorageGroupID >= 3
AND #TEMPTABLE1.fld_StorageGroupID <= 14
)
UNION
SELECT
0,
COUNT(*),
0,
0,
0,
0
FROM
#TEMPTABLE1
WHERE
#TEMPTABLE1.fld_StorageGroupID = 1
UNION
SELECT
0,
0,
COUNT(*),
0,
0,
0
FROM
#TEMPTABLE1
WHERE
#TEMPTABLE1.fld_StorageGroupID = 2
UNION
SELECT
0,
0,
0,
COUNT(*),
0,
0
FROM
#TEMPTABLE2
WHERE
(
#TEMPTABLE2.fld_StorageGroupID >= 3
AND #TEMPTABLE2.fld_StorageGroupID <= 14
)
UNION
SELECT
0,
0,
0,
0,
COUNT(*),
0
FROM
#TEMPTABLE2
WHERE
#TEMPTABLE2.fld_StorageGroupID = 1
UNION
SELECT
0,
0,
0,
0,
0,
COUNT(*)
FROM
#TEMPTABLE2
WHERE
#TEMPTABLE2.fld_StorageGroupID = 2
) A