I have a query as follows:
SELECT c.irn,
pLog.policingname,
ce.*
INTO #caselist
FROM employeereminder_ilog ce
JOIN cases c
ON ce.caseid = c.caseid
JOIN policinglog pLog
ON ( ce.logdatetimestamp BETWEEN
pLog.startdatetime AND pLog.finishdatetime )
WHERE ce.logdatetimestamp BETWEEN @start_pre AND @end_pre
employeereminder_iLOG is a pretty huge table, around 32M rows. POLICINGLOG has around 50 rows. CASES around 0.5m rows. @start_pre and @end_pre are predefined variabled around 30 minutes apart.
This query took around 30 minutes to run, and returns around 600 results.
I was trying to find way to speed up the query by looking at the execution plan. I couldn't work out why however the insert was taking up 99% of the query, as opposed to the select from employeereminder_iLOG .
So, my questions are:
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `cases` ADD INDEX `cases_idx_caseid` (`caseid`);
ALTER TABLE `employeereminder_ilog` ADD INDEX `employeereminder_i_idx_logdatetimestamp` (`logdatetimestamp`);
SELECT
c.irn,
pLog.policingname,
ce.* INTO #caselist
FROM
employeereminder_ilog ce
JOIN
cases c
ON ce.caseid = c.caseid
JOIN
policinglog pLog
ON (
ce.logdatetimestamp BETWEEN pLog.startdatetime AND pLog.finishdatetime
)
WHERE
ce.logdatetimestamp BETWEEN @start_pre AND @end_pre