也许你可以帮助我。我需要查询3个表格才能获取财务股票的数据。一个简单的MySQL查询需要永久(超过20分钟!)
这个想法是去仪器表,找到每个仪器的索引,然后将该特定仪器的所有价格与独立表上的指标结合在一起。
表stockdata
和indicators
都差不多有50.000条记录。 instruments
只是30
这是不正常的查询:
SELECT
indicators.ddate,
instruments.name,
indicators.sma_14,
indicators.sma_5,
stockdata.close
FROM
indicators
INNER JOIN instruments ON (indicators.instrument_id=instruments.id)
INNER JOIN stockdata ON (instruments.name=stockdata.name)
这里是EXPLAIN结果
+----+-------------+-------------+-------+-----------------------------+---------------------+---------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | rows | Extra |
+----+-------------+-------------+-------+-----------------------------+---------------------+---------+------+-------------+
| 1 | SIMPLE | instruments | index | PRIMARY,instruments_index01 | instruments_index01 | 61 | 25 | Using index |
| 1 | SIMPLE | indicators | ref | indicators_index01 | indicators_index01 | 5 | 973 | Using where |
| 1 | SIMPLE | stockdata | ref | stockdata_index01 | stockdata_index01 | 31 | 1499 | Using where |
+----+-------------+-------------+-------+-----------------------------+---------------------+---------+------+-------------+
我真的很感激任何帮助,您可以提供!
这对于参与我的问题表的部分架构:
TABLE `indicators` (
`id` int AUTO_INCREMENT NOT NULL,<br>
`instrument_id` int,
`date` date,
`sma_5` float(10,3),
`sma_14` float(10,3),
`ema_14` float(10,3),
/* Keys */
PRIMARY KEY (`id`)
)
TABLE `instruments` (
`id` int AUTO_INCREMENT NOT NULL,
`name` char(20),
`country` char(50),
`newsquery` char(100),
/* Keys */
PRIMARY KEY (`id`)
)
TABLE `stockdata` (
`id` int AUTO_INCREMENT NOT NULL,
`name` char(10),
`date` date,
`open` float,
`high` float,
`low` float,
`close` float,
`volume` int,
`adjclose` float,
/* Keys */
PRIMARY KEY (`id`)
)
为什么在连接条件周围有括号? – Fosco 2010-07-27 03:53:53
涉及的每个表中有多少行? – sgriffinusa 2010-07-27 03:55:54
你好,表库存数据和指标都差不多有50.000条记录。仪器只有30. – JordanBelf 2010-07-27 03:57:11