我無法工作,如何得到這個子查詢很慢在10K記錄簡單子查詢花費的時間太長,有沒有任何選擇?
table_code獲取數據:
+--------+-----------+------------+
| code_id| code_name | code_date |
+--------+-----------+------------+
| 1 | A1 | 2017-02-01 |
| 2 | A2 | 2017-02-02 |
| 3 | A3 | 2017-02-03 |
| 4 | A4 | 2017-02-04 |
| 5 | A5 | 2017-02-05 |
| 6 | A6 | 2017-02-06 |
| 7 | A7 | 2017-02-07 |
|10000 | A10000 | 2017-02-22 |
+--------+-----------+------------+
table_reg:
+--------+------------+------------+
| reg_id | reg_number | reg_date |
+--------+------------+------------+
| 1 | 1010 | 2017-02-01 |
| 2 | 1020 | 2017-02-02 |
| 3 | 1030 | 2017-02-03 |
| 4 | 1040 | 2017-02-04 |
| 5 | 1050 | 2017-02-05 |
| 6 | 1060 | 2017-02-06 |
| 7 | 1070 | 2017-02-07 |
|10000 | 101010 | 2017-02-22 |
+--------+-----------+------------+
然後我運行:
SELECT
a.`code_name`,
a.`code_date`,
(SELECT b.`reg_number` FROM `table_reg` b WHERE b.`reg_date` <= a.`code_date` ORDER BY b.`reg_date` DESC LIMIT 1) AS `reg_number`,
(SELECT b.`reg_date` FROM `table_reg` b WHERE b.`reg_date` <= a.`code_date` ORDER BY b.`reg_date` DESC LIMIT 1) AS `reg_date`
FROM `table_code` a
結果:
+-----------+------------+------------+------------+
| code_name | code_date | reg_number | reg_date |
+-----------+------------+------------+------------+
| A1 | 2017-02-01 | 1010 | 2017-02-01 |
| A2 | 2017-02-02 | 1020 | 2017-02-02 |
| A3 | 2017-02-03 | 1030 | 2017-02-03 |
| A4 | 2017-02-04 | 1040 | 2017-02-04 |
| A5 | 2017-02-05 | 1050 | 2017-02-05 |
| A6 | 2017-02-06 | 1050 | 2017-02-05 |
| A7 | 2017-02-07 | 1050 | 2017-02-05 |
| A10000 | 2017-02-22 | 1050 | 2017-02-05 |
+-----------+------------+------------|------------+
DDL:
CREATE TABLE `table_reg` (
`reg_id` INTEGER(11) NOT NULL AUTO_INCREMENT,
`reg_number` INTEGER(11) DEFAULT NULL,
`reg_date` DATE DEFAULT NULL,
PRIMARY KEY (`reg_id`) USING BTREE,
KEY `table_reg_idx1` (`reg_date`) USING BTREE
) ENGINE=InnoDB
AUTO_INCREMENT=4 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
COMMENT='InnoDB free: 7168 kB; InnoDB free: 6144 kB';
CREATE TABLE `table_code` (
`code_id` INTEGER(11) NOT NULL AUTO_INCREMENT,
`code_name` VARCHAR(20) COLLATE latin1_swedish_ci DEFAULT NULL,
`code_date` DATE DEFAULT NULL,
PRIMARY KEY (`code_id`) USING BTREE,
KEY `table_code_idx1` (`code_date`) USING BTREE
) ENGINE=InnoDB
AUTO_INCREMENT=8 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
COMMENT='InnoDB free: 7168 kB; InnoDB free: 6144 kB';
結果按預期工作,但它是用10k記錄,
如果code_date不能reg_date_date匹配,它將使用reg_number最新的日期很慢。
(SELECT b.`reg_number` FROM `table_reg` b WHERE b.`reg_date` <= a.`code_date` ORDER BY b.`reg_date` DESC LIMIT 1) AS `reg_number`
是否有任何其他選項查詢?
此鏈接sqlfiddle:http://sqlfiddle.com/#!9/f090ad/1]
任何幫助,將不勝感激。謝謝
您的表格沒有索引。 –
他們有PK's-應該編入索引AFAIK – RuDevel
'WHERE b.reg_date <= a.code_date' - 你至少需要'b.reg_date'上的索引。 –