Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to partition this table
    primarykey
    data
    text
    <p>This is my table</p> <pre><code>CREATE TABLE IF NOT EXISTS `results_stock_all` ( `Result_Id` varchar(50) NOT NULL, `Stock_Date` date NOT NULL, `Stock_Code` varchar(20) NOT NULL, `Prev_Close` float(10,3) NOT NULL DEFAULT '0.000', `Open` float(10,3) NOT NULL DEFAULT '0.000', `Close` float(10,3) NOT NULL DEFAULT '0.000', `High` float(10,3) NOT NULL DEFAULT '0.000', `Low` float(10,3) NOT NULL DEFAULT '0.000', `Volume` int(11) NOT NULL DEFAULT '0', `Change` float(10,3) NOT NULL DEFAULT '0.000', `Change_Percent` float(10,3) NOT NULL PRIMARY KEY (`Result_Id`), KEY `Stock_Date` (`Stock_Date`), KEY `Stock_Code_Stock_Date` (`Stock_Code`,`Stock_Date`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; </code></pre> <p>3,150,236 rows, MYISAM, data size: 255.1MB, index size: 209.5MB, Stock_Code: 1468</p> <p>I have 2 type of query that use often,</p> <p>1) SELECT cols FROM results_stock_all WHERE Stock_Date = '2011-08-06' ORDER BY cols;</p> <p>(No problem, pretty fast)</p> <p>2) SELECT cols FROM results_stock_all WHERE Stock_Code = '1234' ORDER BY Stock_Date DESC LIMIT 3000;</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE results_stock_all ref Stock_Code_Stock_Date Stock_Code_Stock_Date 22 const 5173 Using where </code></pre> <p>I need to query latest 3000 rows and send the results to charting software. But this query takes about 17 secs at first run for each different Stock_Code.</p> <p>How to make it faster ? I am thinking to partition this table, but I really don't have idea how to do it , by Date or by Stock_Code ?</p> <p>If partition by Stock_Code, I believe it will speed up query #2, then how about query #1</p> <p>Thanks.</p> <h2>EDITED</h2> <p>I tried to add an index <code>Stock_Code</code>, it is faster in this query</p> <p>SELECT cols FROM results_stock_all WHERE Stock_Code = '1234' ORDER BY Stock_Date DESC LIMIT 3000;</p> <p>but when i tried to run on smaller results set,</p> <p>mysql> set session query_cache_type = 0;</p> <p>mysql> select * from results_stock_all ignore index(stock_code) where stock_code = '1234' order by stock_date desc; 100 rows in set (0.00 sec)</p> <p>mysql> select * from results_stock_all ignore index(stock_code_stock_date) where stock_code = '1234' order by stock_date desc limit 100; 100 rows in set (0.02 sec)</p> <p>I believe using where is faster than using file sort.</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.
 

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