Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should return the same results and perform slightly faster: </p> <pre><code>SELECT MIN( `quantities`.`start_timestamp` ) AS `start`, MAX( `quantities`.`end_timestamp` ) AS `end`, SUM( `quantities`.`quantity` * `prices`.`price` ) * COUNT(DISTINCT `quantities`.`id`) / COUNT(DISTINCT `prices`.`id`) AS total FROM `quantities` JOIN `prices` ON `prices`.`timestamp` &gt;= `quantities`.`start_timestamp` AND `prices`.`timestamp` &lt; `quantities`.`end_timestamp` AND `prices`.`type_id` = 1 WHERE `quantities`.`start_timestamp` &gt;= '2010-07-01 00:00:00' AND `quantities`.`start_timestamp` &lt; '2010-07-02 00:00:00' GROUP BY HOUR( `quantities`.`start_timestamp` ); </code></pre> <p>Since you can't calculate <code>AVG()</code> inside the <code>SUM()</code>, I had to do some interesting <code>COUNT(DISTINCT)</code> to calculate the number of <code>prices</code> returned per <code>quantities</code>. I'm wondering if this gives you the same results with "real" data...</p> <p>Using <code>JOIN</code>:</p> <pre>+----+-------------+------------+-------+-------------------------------+-----------------+---------+------+-------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+-------+-------------------------------+-----------------+---------+------+-------+----------+----------------------------------------------+ | 1 | SIMPLE | quantities | range | start_timestamp,end_timestamp | start_timestamp | 8 | NULL | 89 | 100.00 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | prices | ALL | timestamp,type_id | NULL | NULL | NULL | 36862 | 62.20 | Using where; Using join buffer | +----+-------------+------------+-------+-------------------------------+-----------------+---------+------+-------+----------+----------------------------------------------+ </pre> <p>vs. the same query only adding <code>LEFT</code> to the <code>JOIN</code></p> <pre>+----+-------------+------------+-------+-------------------+-----------------+---------+-------+-------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+-------+-------------------+-----------------+---------+-------+-------+----------+----------------------------------------------+ | 1 | SIMPLE | quantities | range | start_timestamp | start_timestamp | 8 | NULL | 89 | 100.00 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | prices | ref | timestamp,type_id | type_id | 4 | const | 22930 | 100.00 | | +----+-------------+------------+-------+-------------------+-----------------+---------+-------+-------+----------+----------------------------------------------+ </pre> <p>Interesting that <code>LEFT</code> can completely removes the <code>end_timestamp</code> as a possible key, and changes the selected keys so much, making it take 15 times as long...</p> <p><a href="http://dev.mysql.com/doc/refman/5.0/en/index-hints.html" rel="nofollow noreferrer">This reference page</a> could help you out a little more if you want to look at specifying index hints for your JOINS</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload