简体   繁体   中英

How can I speed up this sql query with substring?

Is there a way to speed up this working query?

I have 4 tables in my database (Area, APeriod, Data, Item). They are setup as follows [column_name (with example)]:

Area [area_code (A102)] [Area_name (Philadelphia-Wilmington-Atlantic City, PA-NJ-DE-MD]

APeriod [period (M01)] [period_abbr (JAN)] [period_name (January)]

Item [item_code (701322)] [item_name (spaghetti and macaroni, per lb. 453.6 gm)]

Data [series_id] [year (1995)] [period (M01)] [value (0.235)] [footnote_codes]

Query Description - Determine the price ratio per pound of steak sirloin [703611] to wine, red and white table (all sizes, any origin; per 1 liter) [702311] in the Miami-FT. Lauderdale area aggregated by month and separately aggregated by years from 1995 to present.

Query Code:

SELECT main.year, APeriod.period_name, steak.value AS Steak, wine.value AS Wine, steak.value/wine.value AS Ratio
FROM
(SELECT DISTINCT year, period FROM Data) main 
INNER JOIN APeriod ON main.period = APeriod.period
LEFT OUTER JOIN Data steak ON steak.series_id ='APU0100703611' AND main.year=steak.year AND main.period=steak.period
LEFT OUTER JOIN Data wine ON wine.series_id ='APU0400720311' AND main.year=wine.year AND main.period=wine.period
ORDER BY main.year, main.period

This query works but it takes the MySQL server on average 129 seconds to run this query (which i'm told is ridiculously long). Example of return

[Showing rows 0 - 24 (209 total, Query took 137.6224 seconds.) [year: 1995 - 1997]

year period_name Steak Wine Ratio

1995 January 3.593 NULL NULL
1995 February3.510 NULL NULL
1995 March 3.708 NULL NULL
1995 April 3.747 NULL NULL
1995 May 3.462 NULL NULL
1995 June 3.742 NULL NULL
1995 July 3.686 4.661 0.7908174
1995 August 3.823 3.978 0.9610357
1995 Septemb 3.625 4.580 0.7914847
1995 October 3.795 4.042 0.9388916
1995 November3.509 4.760 0.7371849
1995 December3.315 4.056 0.8173077
(couldn't insert image properly, sorry)

I tried taking the substring and creating a view to help speed things up but encountered other errors which leads me to believe that I did something wrong as I am new to sql. Is there a way to speed this up? If so, please show me. Thanks in advance.

This is your query:

SELECT main.year, APeriod.period_name, steak.value AS Steak, wine.value AS Wine,
       steak.value/wine.value AS Ratio
FROM (SELECT DISTINCT year, period FROM Data) main INNER JOIN
     APeriod
     ON main.period = APeriod.period LEFT OUTER JOIN
     Data steak
     ON steak.series_id ='APU0100703611' AND
        main.year=steak.year AND
        main.period=steak.period LEFT OUTER JOIN
     Data wine
     ON wine.series_id ='APU0400720311' AND
        main.year=wine.year AND
        main.period=wine.period
ORDER BY main.year, main.period;

Indexes should be a big help. I would suggest:

create index idx_data_year_period_series on data(year, period, seies);
create index idx_aperiod_period on aperiod(period);

The second of these (on aperiod.period ) already exists if aperiod.period is declared either as a primary key or unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM