哪个SQL语句更快?
SELECT TOP 2 c1.Price, c2.Price, ..... c49.Price, c50.Price FROM Table1 AS c1, Table2 AS c2, ..... Table49 AS c49, Table50 AS c50 WHERE c1.Date = c2.Date AND c2.Date = c3.Date ..... c49.Date = c50.Date ORDER BY c1.ID DESC OR SELECT TOP 2 c1.Price, c2.Price, ..... c49.Price, c50.Price FROM (Table1 AS c1 INNER JOIN (Table2 AS c2 ........ INNER JOIN (Table49 AS c49 INNER JOIN Table50 AS c50 ON c49.Date = c50.Date) ........ ON c2.FullDate__ = c3.FullDate__) ON c1.FullDate__ = c2.FullDate__) ORDER BY c1.ID DESC";
基本上我需要从每个表中提取2行以定期生成摘要.哪种说法更快?
更快的是没有50个表开始.加入50个表可能没问题,但这是一个非常反直觉的设计,可能不是最易维护的解决方案.
您是否可以将数据存储在单个(或更少)表的行(或列)中,而不是50个表中?
通常会更好,但最好的方法是逐个案例并将其投入分析器,或更简单但显示执行计划.Folk通常对理论上哪种方法最快/最好的方法有很强烈的意见,但根据您实际处理的数据实际调整没有替代,因为适用的理论根据您的数据负载而变化.
如果您的应用尚未拥有真实数据,请尝试创建一些逼真的压力数据.这将继续对测试有用.然后在应用程序生效后安排调整时间.