0

I have this query here that I am trying to make more efficient as it is taking an extremely long amount of time, about 40s on 3 million records.

The query basically takes X amount of rows per company and turns them into columns for a given existing column.

SQLFiddle

SET @sql = NULL;
SET @sql1 = NULL;
SET @sql2 = NULL;
SET @sql3 = NULL;
SET @sql4 = NULL;
SET @sql5 = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when year = ', year, ' then experience_rate end) AS `', year, '-Pen`' ) ORDER BY year ) INTO @sql1 FROM spooner_pec;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when year = ', year, ' then mco_name end) AS `', year, '-MCO`' ) ORDER BY year ) INTO @sql2 FROM spooner_pec;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when year = ', year, ' then premium_range end) AS `', year, '-Prem`' ) ORDER BY year ) INTO @sql3 FROM spooner_pec;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when year = ', year, ' then employer_rating_plan end) AS `', year, '-Rating`' ) ORDER BY year ) INTO @sql4 FROM spooner_pec;
SELECT GROUP_CONCAT(DISTINCT CONCAT( 'max(case when year = ', year, ' then risk_group_number end) AS `', year, '-Gr Num`' ) ORDER BY year ) INTO @sql5 FROM spooner_pec;
SET @sql = CONCAT( 
    'SELECT policy_number AS PolicyNumber, 
    coverage_status_code As CoverageStatusCode, 
    primary_name AS PrimaryName, 
    primary_dba_name AS DBA, 
    address1 AS Address1, 
    address2 AS Address2, 
    city AS CityName, 
    state AS StateID, 
    zipcode AS ZipCode, 
    zip_plus_four AS ZipCode4, 
    business_area_code AS PhoneAreaCode, 
    business_phone AS PhoneNumber, 
    business_extension AS PhoneExtension, 
    business_contact_first_name AS FirstName, 
    business_contact_last_name AS LastName, 
    county_description AS County, ', @sql1, ', ', @sql2, ',', @sql3, ',', @sql4, ',', @sql5, ' 
    FROM spooner_pec GROUP BY policy_number');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

One of my coworkers mentioned indexing but I think the columns that are taking the longest are the ones that I am creating to pivot the data, is there any way to index those?

Brandon Shega
  • 769
  • 4
  • 17
  • What are your current indexes? At the very least, `spooner_pec.year` should be an index if you want this to run in a reasonable amount of time. – TheGentleman Aug 27 '15 at 20:49
  • I added indexes for every column in the large select statement, it actually seems to be running a lot faster now, down to about 10 seconds, is that what is needed? – Brandon Shega Aug 27 '15 at 20:52
  • Indexing `policy_number` would have helped, the others probably didn't have a large effect. Indexing only increases performance when you are doing something that requires iterating over a lot of data e.g. `WHERE` and `Group By` clauses. Once you've got your filter, selecting the rest of the row isn't as big a deal. I recommend giving [this](http://stackoverflow.com/questions/1108/how-does-database-indexing-work) a read. – TheGentleman Aug 27 '15 at 20:55
  • Great thanks, if you want to make that as an answer, I will gladly accept. Thank you for your help! – Brandon Shega Aug 27 '15 at 20:59

0 Answers0