English 中文(简体)
SQL with terribl terribl
原标题:SQL query with subqueries performing terribly

我有这么长时间的问询,应该向我提供一些有关运输的信息,而且它运作良好,但表现得非常糟糕。 装载量大约为4 500米。

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;

任何想法,我会做什么错做,或我能做些什么? 我有其他类似时间的询问,没有这么多时间来装载。 我认为,这样做的速度将快于例如,每天都有个人询问(在我的方案规划中,而不是在结构上)。

最佳回答

这是因为你正在使用不需要的分局。

作为一项一般规则,如果你在主要选任合同条款内有一个分局,则分局将质疑选任主选任条款中各行各一份的表格,这样,如果你有7个分局,并且选择30天的时间范围,你将有效地管理210个分局(加上你的主要询问)。

(某些情况下,有些问题选择异构体会将分类排入主要问题,但作为一项一般规则,你可能不依赖此。)

在这种情况下,你不需要<条码><>条码/代码>的子序列,因为所有<条码>条码/代码>。 您要求的数据载于主要询问中,因此,你可以将数据改成:

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;
问题回答

你的问询中的问题是,这 table了同一桌子。ORDER表的所有扫描(如果是的话)可改为多个SUM+CASE或COUNT+CASE,如。 查询和案件说明





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签