Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: mySQL - a freakish query. Can you optimize this? a challenge

  1. #1
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    Question mySQL - a freakish query. Can you optimize this? a challenge

    Have to create a report, which displays each district Manager, his Name, and the sum of the prices he sells at. Each part we have has 7 price levels they can sell at. they may also raise the price slightly to make commission. the prices are in inventory_pricing, and when ordered high to low, are like this:
    p4,p3,p2,p5,p6,p7,p1

    Anyways, here is my current query, which works exactly as I need it to. However, as you could imagine, it is slow. It needs to be optimized. Anyone care to try?

    Code:
    select p4.DistrictManager, s.Name, p4.Price4, p3.Price3, p2.Price2, p5.Price5, p6.Price6, p7.Price7, p1.Price1
    FROM
       (Select ROUND(SUM(l.Price), 2) AS Price4, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price4) group by h.DistrictManager) AS p4
       inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price3, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price3) and (l.Price < i.Price4) group by h.DistrictManager) AS p3
       on p4.districtmanager = p3.districtmanager inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price2, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price2) and (l.Price < i.Price3) group by h.DistrictManager) AS p2
       on p3.districtmanager = p2.districtmanager inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price5, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price5) and (l.Price < i.Price2) group by h.DistrictManager) AS p5
       on p2.districtmanager = p5.districtmanager inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price6, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price6) and (l.Price < i.Price5) group by h.DistrictManager) AS p6
       on p5.districtmanager = p6.districtmanager inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price7, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price7) and (l.Price < i.Price6) group by h.DistrictManager) AS p7
       on p6.districtmanager = p7.districtmanager inner join
    
       (Select ROUND(SUM(l.Price), 2) AS Price1, h.DistrictManager from inventory_pricing i inner join invoices_salesline l on l.PartNumber = i.PartNumber
       inner join invoices_saleshead h on l.InvoiceNumber = h.InvoiceNumber
       where (l.Price >= i.Price1) and (l.Price < i.Price7) group by h.DistrictManager) AS p1
       on p1.districtmanager = p7.districtmanager inner join
    
    sales_districtmanagers s on p7.districtmanager = s.districtmanager
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    And now for the fun part.... I have to add a few variables- start date, end date (which is l.InvoiceDate), and whether or not the DM is an actual sales rep, or just a territory (s.Human [logical])


    AND THE REAL KICKER
    I Have to convert it to SQL!

    RAWR!!!!!!!!!!
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  4. #3
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Ok, got it into SQL, added the variables, which this will be used in VS2008. But... still wonder if it could be optimized, although now that it is in SQL, it seriously takes about 1 second versus the few minutes it took in mySQL.... glad I could help myself lol.
    But seriously- if you have any ideas how to optimize the following, feel free to say so

    Code:
    SELECT     p4.DistrictManager, s.Name, SUM(p4.Price4) AS Price4, SUM(p3.Price3) AS Price3, SUM(p2.Price2) AS Price2, SUM(p5.Price5) AS Price5, 
                          SUM(p6.Price6) AS Price6, SUM(p7.Price7) AS Price7, SUM(p1.Price1) AS Price1
    FROM         (SELECT     ROUND(SUM(l.Price), 2) AS Price4, h.DistrictManager
                           FROM          inventory_pricing AS i INNER JOIN
                                                  invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price4 <= l.Price INNER JOIN
                                                  invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                           WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                           GROUP BY h.DistrictManager) AS p4 INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price3, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price3 <= l.Price AND i.Price4 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p3 ON p4.DistrictManager = p3.DistrictManager AND p4.Price4 > p3.Price3 AND p4.Price4 > p3.Price3 INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price2, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price2 <= l.Price AND i.Price3 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p2 ON p3.DistrictManager = p2.DistrictManager INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price5, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price5 <= l.Price AND i.Price2 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p5 ON p2.DistrictManager = p5.DistrictManager INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price6, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price6 <= l.Price AND i.Price5 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p6 ON p5.DistrictManager = p6.DistrictManager INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price7, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price7 <= l.Price AND i.Price6 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p7 ON p6.DistrictManager = p7.DistrictManager INNER JOIN
                              (SELECT     ROUND(SUM(l.Price), 2) AS Price1, h.DistrictManager
                                FROM          inventory_pricing AS i INNER JOIN
                                                       invoices_salesline AS l ON l.PartNumber = i.PartNumber AND i.Price1 <= l.Price AND i.Price7 > l.Price INNER JOIN
                                                       invoices_saleshead AS h ON l.InvoiceNumber = h.InvoiceNumber
                                WHERE      (l.InvoiceDate BETWEEN @startDate AND @endDate)
                                GROUP BY h.DistrictManager) AS p1 ON p1.DistrictManager = p7.DistrictManager INNER JOIN
                          sales_districtmanagers AS s ON p7.DistrictManager = s.DistrictManager
    WHERE     (s.Human <> @human)
    GROUP BY p4.DistrictManager, s.Name
    ORDER BY p4.DistrictManager
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  5. #4
    Jordan Guest

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Wow, that is one long query.

  6. #5
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Holy **** dude!

  7. #6
    djemmers's Avatar
    djemmers is offline Newbie
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    12
    Rep Power
    0

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Quote Originally Posted by Parabola View Post
    although now that it is in SQL, it seriously takes about 1 second versus the few minutes it took in mySQL.... glad I could help myself lol.
    I know this is a bit off topic but...
    This I don't understand.
    what do you mean?
    I thought sql was the language and mySQL the program?
    So how can you change the query from mysql to sql?

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Quote Originally Posted by djemmers View Post
    I know this is a bit off topic but...
    This I don't understand.
    what do you mean?
    I thought sql was the language and mySQL the program?
    So how can you change the query from mysql to sql?
    Well, each server system has their own dialects of SQL, with add ons and sometimes a little bit different way of expressing certain things. the pure statements normally is very similar, but when it comes to other things as calculation functions and so, it starts to differ, or just limitations of results on how many rows to return differs between the different systems.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  9. #8
    djemmers's Avatar
    djemmers is offline Newbie
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    12
    Rep Power
    0

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    ah ok , I was affraid that I missed something crucial and made all my queries slow up till now ;-)

  10. #9
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    Basically here's the reason why SQL is better: mySQL is free. You can get mySQL server and all that stuff for free, which is ok for small things, but not large companies. Microsoft SQL however is not free. It costs a bit. But, that also means you get a better running server. And as Orjan explained, 2 different companies means 2 different dialects of the language. For example, c++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs (courtesy wikipedia), where as c# (of course developed later) was developed by Microsoft within the .NET initiative (courtesy wikipedia). Developed by two different groups, so they are slightly different. I know they aren't that close, but I say they are similar in that... well they are lol.

    It's just how programming is... there are dialects of many different languages lol
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

  11. #10
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: mySQL - a freakish query. Can you optimize this? a challenge

    No way, SQL is the Language and is a standard. MySQL IS the Program as is MS SQL. There are slight function differences for non-standard functions but SQL is suppose to be universal.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help on a MySql Query
    By webcodez in forum Database & Database Programming
    Replies: 4
    Last Post: 03-06-2011, 05:33 AM
  2. MySql query updates twice
    By hampus.tagerud in forum PHP Development
    Replies: 6
    Last Post: 10-19-2010, 02:47 PM
  3. MySQL with PHP Complicated Query
    By j.smith1981 in forum PHP Development
    Replies: 2
    Last Post: 08-21-2009, 02:01 AM
  4. MySQL Query Errors
    By Brandon W in forum Database & Database Programming
    Replies: 4
    Last Post: 02-15-2009, 02:36 AM
  5. MySQL Inner Join Query...
    By Lop in forum Database & Database Programming
    Replies: 3
    Last Post: 01-11-2007, 01:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts