Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:29:40+00:00 2026-05-27T00:29:40+00:00

I have this quite long query that should give me some information about shipments,

  • 0

I have this quite long query that should give me some information about shipments, and it works, but it’s performing terribly bad. It takes about 4500ms to load.

SELECT
    DATE(paid_at) AS day,
    COUNT(*) as order_count,
    (
      SELECT COUNT(*) FROM line_items
      WHERE order_id IN (SELECT id from orders WHERE DATE(paid_at) = day)
    ) as product_count,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'colissimo'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
    ) as orders_co,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'colissimo'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
      AND paid_amount < 70
    ) as co_less_70,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'colissimo'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
      AND paid_amount >= 70
    ) as co_plus_70,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'mondial_relais'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
    ) as orders_mr,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'mondial_relais'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
      AND paid_amount < 70
    ) as mr_less_70,
    (
      SELECT COUNT(*) FROM orders
      WHERE shipping_method = 'mondial_relais'
      AND DATE(paid_at) = day
      AND state IN ('paid','shipped','completed')
      AND paid_amount >= 70
    ) as mr_plus_70
    FROM orders
    WHERE MONTH(paid_at) = 11
    AND YEAR(paid_at) = 2011
    AND state IN ('paid','shipped','completed')
    GROUP BY day;

Any idea what I could be doing wrong or what I could be doing better? I have other queries of similar length that don’t take as much time to load as this. I thought this would be faster than for example having an individual query for each day (in my programming instead of the SQL query).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:29:41+00:00Added an answer on May 27, 2026 at 12:29 am

    It is because you are using sub-queries where you don’t need them.

    As a general rule, where you have a sub-query within a main SELECT clause, that sub-query will query the tables within it once for each row in the main SELECT clause – so if you have 7 subqueries and are selecting a date range of 30 days, you will effectively be running 210 separate subqueries (plus your main query).

    (Some query optimisers can resolve sub-queries into the main query under some circumstances, but as a general rule you can’t rely on this.)

    In this case, you don’t need any of the orders sub-queries, because all the orders data you require is included in the main query – so you can rewrite this as:

    SELECT
        DATE(paid_at) AS day,
        COUNT(*) as order_count,
        (
          SELECT COUNT(*) FROM line_items
          WHERE order_id IN (SELECT id from orders WHERE DATE(paid_at) = day)
        ) as product_count,
        sum(case when shipping_method = 'colissimo' then 1 end) as orders_co,
        sum(case when shipping_method = 'colissimo' AND 
                      paid_amount < 70 then 1 end) as co_less_70,
        sum(case when shipping_method = 'colissimo' AND 
                      paid_amount >= 70 then 1 end) as co_plus_70,
        sum(case when shipping_method = 'mondial_relais' then 1 end) as orders_mr,
        sum(case when shipping_method = 'mondial_relais' AND 
                      paid_amount < 70 then 1 end) as mr_less_70,
        sum(case when shipping_method = 'mondial_relais' AND 
                      paid_amount >= 70 then 1 end) as mr_plus_70
        FROM orders
        WHERE MONTH(paid_at) = 11
        AND YEAR(paid_at) = 2011
        AND state IN ('paid','shipped','completed')
        GROUP BY day;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is probably trivial, but i have a quite long sql query and echo
I have quite a long query that is causing me some problems. For the
I have this quite popular problem, but have failed to find a solution that
This should be quite easy, and I have done some research on this. I
I have this query that works: select name, location_id from all_names where location_id in
I have read this question but it's not quite what I was looking for.
I have been struggling with this for quite some time having been accustomed to
Hell All, So I have been facing this behavior for quite some time and
This question is about a general technique in SQL, that I can't quite work
I am trying to speed up a long running query that I have (takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.