TL;DR:正确的索引可能会更好地为您服务。卸载JOIN
到一个函数实际上可能会使事情变得更糟。但是正确索引 aVIEW
并不简单,并且您没有提供足够的信息来提供有保证的解决方案。下面,您将找到一个提案和一个要评估的测试。
可能是函数正在返回,但它需要很长时间。我让它与原始查询大约在同一时间返回(我的定义与您的不同,请仔细检查我的代码,我可能误解了):
样本数据
CREATE TABLE CRM_CUSTOMER_VIEW
( CUSTOMER_ID INTEGER, COMPANYNAME VARCHAR(50) );
INSERT INTO CRM_CUSTOMER_VIEW VALUES ( 1, 'ACME' ), ( 2, 'NASA' );
SELECT @N:=COUNT(*) FROM CRM_CUSTOMER_VIEW;
INSERT INTO CRM_CUSTOMER_VIEW SELECT CUSTOMER_ID + @N, CONCAT(SUBSTRING(COMPANYNAME, 1, 4), ' ', @N, '.', CUSTOMER_ID)
FROM CRM_CUSTOMER_VIEW;
-- Repeat the two rows above to fill the table with, say, half a million records.
CREATE TABLE CRM_RESALE ( CUSTOMER_ID INTEGER, RESALE_ID INTEGER );
SELECT @N:=1;
INSERT INTO CRM_RESALE SELECT CUSTOMER_ID, 5*CUSTOMER_ID+@N FROM CRM_CUSTOMER_VIEW;
SELECT @N:=@N+1;
-- Repeat five times the two rows above to get five items per customer
CREATE TABLE CRM_RESALE_ITEM_VIEW ( RESALE_ID INTEGER, RRP NUMERIC(7,3));
INSERT INTO CRM_RESALE_ITEM_VIEW SELECT RESALE_ID, 3.14159 FROM CRM_RESALE;
现在我们运行查询以获取基线 - 没有索引,因此非常昂贵,即使在快速机器上也是如此
SELECT COMPANYNAME,
(
SELECT SUM(RRP) * 0.1
FROM CRM_RESALE_ITEM_VIEW
INNER JOIN CRM_RESALE using (RESALE_ID)
WHERE CRM_RESALE.CUSTOMER_ID = CRM_CUSTOMER_VIEW.CUSTOMER_ID
) AS DERRIVED_MAINTENANCE
FROM CRM_CUSTOMER_VIEW WHERE COMPANYNAME = 'ACME';
+-------------+----------------------+
| COMPANYNAME | DERRIVED_MAINTENANCE |
+-------------+----------------------+
| ACME | 1.5710 |
+-------------+----------------------+
1 row in set (3.18 sec)
现在我们将内部查询移动到它自己的函数中。
DELIMITER //
CREATE FUNCTION DERRIVED_MAINTENANCE ( CUSTID INTEGER )
RETURNS NUMERIC(7,3) DETERMINISTIC
BEGIN
SELECT SUM(RRP) * 0.1 INTO @SRRP
FROM CRM_RESALE_ITEM_VIEW
INNER JOIN CRM_RESALE using (RESALE_ID)
WHERE CRM_RESALE.CUSTOMER_ID = CUSTID;
RETURN @SRRP;
END//
DELIMITER ;
mysql> SELECT DERRIVED_MAINTENANCE(1);
+-------------------------+
| DERRIVED_MAINTENANCE(1) |
+-------------------------+
| 1.571 |
+-------------------------+
1 row in set (3.60 sec)
如果我对五行运行查询,我得到的时间是函数被调用五次后的五倍。
SELECT CUSTOMER_ID, DERRIVED_MAINTENANCE(CUSTOMER_ID) FROM CRM_CUSTOMER_VIEW WHERE CUSTOMER_ID < 5;
+-------------+-----------------------------------+
| CUSTOMER_ID | DERRIVED_MAINTENANCE(CUSTOMER_ID) |
+-------------+-----------------------------------+
| 1 | 1.571 |
| 2 | 1.571 |
| 3 | 1.571 |
| 4 | 1.571 |
+-------------+-----------------------------------+
4 rows in set (14.45 sec)
但是,如果我使用覆盖索引对表进行索引——我可以这样做,因为它们是表;您也可以对视图执行此操作,但是您需要以不同的方式索引,也许您可以从不同的聚合视图中受益。在不了解更多信息的情况下无法真正提供建议
CREATE INDEX CRM_RESALE_ITEM_VIEW_NDX ON CRM_RESALE_ITEM_VIEW(RESALE_ID, RRP);
CREATE INDEX CRM_RESALE_NDX ON CRM_RESALE (CUSTOMER_ID, RESALE_ID);
现在我可以使用 aVIEW
代替函数,或者调用函数:
CREATE VIEW CRM_RESALE_FULL_VIEW AS
SELECT CUSTOMER_ID, SUM(RRP) * 0.1 AS DERRIVED_MAINTENANCE
FROM CRM_RESALE_ITEM_VIEW
INNER JOIN CRM_RESALE using (RESALE_ID)
GROUP BY CUSTOMER_ID;
SELECT COMPANYNAME, DERRIVED_MAINTENANCE FROM CRM_CUSTOMER_VIEW JOIN CRM_RESALE_FULL_VIEW USING (CUSTOMER_ID) WHERE COMPANYNAME LIKE 'ACME 1024.20%';
+---------------+----------------------+
| COMPANYNAME | DERRIVED_MAINTENANCE |
+---------------+----------------------+
| ACME 1024.201 | 1.5710 |
| ACME 1024.203 | 1.5710 |
| ACME 1024.205 | 1.5710 |
| ACME 1024.207 | 1.5710 |
| ACME 1024.209 | 1.5710 |
+---------------+----------------------+
5 rows in set (1.11 sec)
SELECT COMPANYNAME, DERRIVED_MAINTENANCE FROM CRM_CUSTOMER_VIEW JOIN CRM_RESALE_FULL_VIEW USING (CUSTOMER_ID) WHERE COMPANYNAME LIKE 'ACME 1024.2%';
+---------------+----------------------+
| COMPANYNAME | DERRIVED_MAINTENANCE |
+---------------+----------------------+
| ACME 1024.21 | 1.5710 |
...
| ACME 1024.299 | 1.5710 |
+---------------+----------------------+
55 rows in set (1.31 sec)
或调用函数
SELECT CUSTOMER_ID, DERRIVED_MAINTENANCE(CUSTOMER_ID) FROM CRM_CUSTOMER_VIEW WHERE CUSTOMER_ID > 42 AND CUSTOMER_ID < 47;
SELECT CUSTOMER_ID, DERRIVED_MAINTENANCE(CUSTOMER_ID) FROM CRM_CUSTOMER_VIEW WHERE CUSTOMER_ID > 42 AND CUSTOMER_ID < 47;
+-------------+-----------------------------------+
| CUSTOMER_ID | DERRIVED_MAINTENANCE(CUSTOMER_ID) |
+-------------+-----------------------------------+
| 43 | 1.571 |
| 44 | 1.571 |
| 45 | 1.571 |
| 46 | 1.571 |
+-------------+-----------------------------------+
4 rows in set (0.03 sec)
单独的索引可以产生两个数量级的性能提升。