Git Product home page Git Product logo

Comments (2)

huayanYu avatar huayanYu commented on June 2, 2024

在A表数据量巨大的情况下,两个SQL语句的执行速度可能会受到不同因素的影响。为了确定哪种SQL可能执行得更快,我们需要考虑以下几个关键点:

查询条件的位置:

SQL1在JOIN操作中就对B和C表的tenant_id进行了过滤,这可能会减少需要JOIN的数据量,从而减少了最终需要处理的数据量。
SQL2将所有的过滤条件都放在了WHERE子句中,这意味着在执行JOIN操作之前,不会对B和C表的数据进行过滤。
索引的使用:

如果tenant_id字段在B和C表上有索引,SQL1可能会利用这个索引来快速过滤出tenant_id = -1的行,这可能加快了JOIN操作的速度。
SQL2可能会在完成JOIN操作后,再应用WHERE子句中的过滤条件,这可能意味着需要处理更多的数据。
数据库优化器:

不同的数据库优化器可能会采用不同的策略来执行查询。有些优化器可能会识别出在JOIN条件中过滤tenant_id可以减少数据量,而有些优化器可能会等到所有JOIN操作完成后再应用过滤条件。
数据的分布:

如果tenant_id = -1的记录在B和C表中非常少,那么SQL1可能会更快,因为它可以提前过滤掉不符合条件的记录。
如果tenant_id = -1的记录在B和C表中占大多数,那么SQL2可能不会比SQL1慢太多,因为最终需要处理的数据量相似。
查询计划:

执行前,数据库优化器会为每个查询生成一个查询计划。查询计划会展示优化器将如何执行查询,包括是否使用索引、JOIN的顺序等。查看查询计划可以帮助我们理解为什么一个查询比另一个查询更快。
其他因素:

服务器的硬件性能、数据库的配置、并发查询的数量等也会影响查询的执行速度。
基于上述因素,理论上,如果B和C表中的tenant_id = -1的记录相对较少,并且数据库优化器能够识别并利用这一点,SQL1可能会更快,因为它可以在JOIN操作中提前过滤掉不符合条件的记录。然而,这只是一种可能的情况,实际的执行速度还需要依赖于具体的数据库优化器和数据的实际情况。

为了得到准确的答案,最好的方法是在目标数据库上对这两个查询进行实际的性能测试。这可以通过执行EXPLAIN命令(或等效的命令,取决于你使用的数据库系统)来查看查询计划,或者直接通过SELECT语句运行查询并测量执行时间来完成。

from mybatis-plus.

miemieYaho avatar miemieYaho commented on June 2, 2024

你要的sql不合理

from mybatis-plus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.