加速左连接查询

时间:2015-05-12 15:49:51

标签: mysql database performance

我有这个查询从几个表中获取一些数据。查询工作正常,但服务器需要很长时间才能获得结果。

$query_serial       = "SELECT serienummers.serienummer, salesregels.aantalartikelen FROM serienummers
                            LEFT JOIN 
                            (
                                SELECT aantalartikelen, artikelcode, salescode FROM salesregels
                            )salesregels ON salesregels.salescode = serienummers.salescode
                            LEFT JOIN 
                            (
                                SELECT factuurnummer, code FROM sales
                            )sales ON sales.code = serienummers.salescode                            
                            WHERE serienummer = '$serialnumber' AND salesregels.artikelcode = serienummers.artikelcode AND sales.factuurnummer != '-1'
                            ";

我已经在线阅读了一些关于索引表的内容,但我不太清楚如何应用它。有人能指出我正确的方向吗?

我的桌子:

Serienummers:
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| code          | int(11)      | NO   | PRI | 0       |       |
| serienummer   | varchar(100) | NO   |     |         |       |
| artikelcode   | int(11)      | YES  |     | 0       |       |
| datum         | int(11)      | NO   |     | 0       |       |
| usercode      | int(11)      | NO   |     | 0       |       |
| salescode     | int(11)      | YES  |     | 0       |       |
| reparatiecode | int(11)      | YES  |     | 0       |       |
| actie         | varchar(250) | NO   |     |         |       |
| rmacode       | int(11)      | NO   |     | 0       |       |
+---------------+--------------+------+-----+---------+-------+
Salesregels
+---------------------------+-------------------------------------------------+-
-----+-----+-----------+-------+
| Field                     | Type                                            |
Null | Key | Default   | Extra |
+---------------------------+-------------------------------------------------+-
-----+-----+-----------+-------+
| code                      | int(11)                                         |
NO   | PRI | 0         |       |
| salescode                 | int(11)                                         |
NO   |     | 0         |       |
| aantalartikelen           | double                                          |
NO   |     | 0         |       |
| artikelcode               | int(11)                                         |
NO   |     | 0         |       |
| prijsperstukinclbtw_old   | varchar(20)                                     |
NO   |     | 0         |       |
| btwtypecode               | int(11)                                         |
NO   |     | 0         |       |
| artikeltekst              | varchar(250)                                    |
NO   |     |           |       |
| procentbtw_old            | varchar(10)                                     |
NO   |     | 0,00      |       |
| isprijsonderdrukt         | int(11)                                         |
NO   |     | 0         |       |
| volgordenummer            | int(11)                                         |
NO   |     | 0         |       |
| prijsperstukinclbtw       | double                                          |
NO   |     | 0         |       |
| procentbtw                | double                                          |
NO   |     | 0         |       |
| korting                   | double                                          |
NO   |     | 0         |       |
| kortingtype               | enum('procenten','eurosinclbtw','eurosexclbtw') |
NO   |     | procenten |       |
| inkoopprijsperstukexclbtw | double                                          |
NO   |     | 0         |       |
| inkoopprijsprocentbtw     | double                                          |
NO   |     | 0         |       |
| inkoopprijsbtwtypecode    | int(11)                                         |
NO   |     | 0         |       |
| dagengarantie             | int(11)                                         |
NO   |     | 0         |       |
+---------------------------+-------------------------------------------------+-
Sales
+-------------------------+--------------+------+-----+---------+-------+
| Field                   | Type         | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+-------+
| code                    | int(11)      | NO   | PRI | 0       |       |
| salestypecode           | int(11)      | NO   |     | 0       |       |
| websitesalesidcode      | int(11)      | NO   |     | 0       |       |
| datum                   | int(11)      | NO   |     | 0       |       |
| datumbetaald            | int(11)      | NO   |     | 0       |       |
| datumlaatstgewijzigd    | int(11)      | NO   |     | 0       |       |
| usercodelaatstgewijzigd | int(11)      | NO   |     | 0       |       |
| adrescode               | int(11)      | NO   |     | 0       |       |
| usercode                | int(11)      | NO   |     | 0       |       |
| bedragvoldaan1_old      | varchar(15)  | NO   |     | 0       |       |
| bedragvoldaan1          | double       | NO   |     | 0       |       |
| betalingswijzecode1     | int(11)      | NO   |     | 0       |       |
| bedragvoldaan2_old      | varchar(15)  | NO   |     | 0       |       |
| bedragvoldaan2          | double       | NO   |     | 0       |       |
| betalingswijzecode2     | int(11)      | NO   |     | 0       |       |
| opmerkingen             | longtext     | NO   |     | NULL    |       |
| opmerkingenextern       | longtext     | YES  |     | NULL    |       |
| voorraadlokatiecode     | int(11)      | NO   |     | 0       |       |
| computernaam            | varchar(255) | YES  |     | NULL    |       |
| factuurnummer           | bigint(20)   | NO   |     | 0       |       |
| betaald                 | int(11)      | NO   |     | 1       |       |
| status                  | varchar(255) | YES  |     | NULL    |       |
| dagenbetalingstermijn   | int(11)      | NO   |     | 8       |       |
| externordernummer       | varchar(255) | YES  |     | NULL    |       |
| externfactuurnummer     | varchar(255) | YES  |     | NULL    |       |
| isdeleted               | int(11)      | NO   |     | 0       |       |
+-------------------------+--------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:1)

在执行join之前,您不需要使用内部查询选择所有内容。 连接可以直接完成。

select
sr.serienummer,
sg.aantalartikelen
from serienummers sr
left join salesregels sg on sg.salescode = sr.salescode and sg.artikelcode = sr.artikelcode
left join sales s on s.code = sr.salescode
where 
sr.serienummer = '$serialnumber'
and s.factuurnummer != '-1' ;

现在加快你需要在where子句中使用的连接键和键的索引。

alter table serienummers add index sales_kel_code(salescode,artikelcode);
alter table serienummers add index serienummer_idx(serienummer);
alter table salesregels add index salescode_idx(salescode);
alter table sales add index factuurnummer_idx(factuurnummer);