需要提高sql查询的性能

发布于 2024-12-22 03:26:57 字数 276 浏览 0 评论 0原文

我有 3 个表 a、b、c。

select * from a

select * from b where aid in (select id from a)

select * from c where bid in (select Id from b where aid in (select id from a))

这些查询在 sp 中工作正常,但作为性能,我需要优化它们。 您能否建议我如何提高性能,或任何可用的工具 优化sql查询。

谢谢 。

I have 3 table a,b,c.

select * from a

select * from b where aid in (select id from a)

select * from c where bid in (select Id from b where aid in (select id from a))

These queries in sp and working fine but as performance I need to optimise these.
Could you please suggest how do I improve perfomance, or any tools available to
optimise sql queries.

Thanks .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

︶ ̄淡然 2024-12-29 03:26:57

我认为你可以使用INNER JOIN重写查询:

select * from c 
INNER JOIN b ON b.id = c.bid 
INNER JOIN a ON a.id = b.aid

如果你有id、bid和aid索引,一切都应该没问题

I think you could rewrite the queries using INNER JOIN:

select * from c 
INNER JOIN b ON b.id = c.bid 
INNER JOIN a ON a.id = b.aid

if you have indexes on id, bid and aid all should be fine

奢望 2024-12-29 03:26:57

查询速度缓慢的原因可能有很多。从您所描述的几个出发点可能是:

  • 索引丢失或不正确。至少,您必须在您连接或过滤的列上建立索引。
  • 索引的统计信息必须是最新的
  • 在某些情况下,将更多字段作为包含列可以使索引受益。
  • 返回的数据量正确 - 无论是列还是行。如果返回太多,就会淹没各个缓冲区并降低整体性能。

您可能需要使用 Tuning Advisor 和/或 SQL Server Profiler - 两者都可以在启动时的 SQL Server 文件夹下的性能工具菜单中找到。

此外,了解 Management Studios 报告的可能性并熟悉执行计划也是很好的起点。

对于更高级的学习者,开始包括查询 I/O 的统计信息(在 Management Studios 查询窗口中),使用 Windows 性能监视器来关注相关的 SQL Server 计数器等。可以在此处找到其中一些的很好的解释:http://www.sql-server-performance.com/2005/sql-server-performance-monitor-coutners/

There can be many reasons why your queries are slow. From what you describe a few starting points could be:

  • Indexes missing or incorrect. At the very least you must have indexes on columns that you join or filter on.
  • Statistics for indexes must be up-to-date
  • Indexes could in some cases benefit from having more fields as included columns.
  • Correct amount of data returned - both in terms of columns and rows. If you return too much, you'll flood the various buffers and reduce overall performance.

You might want to use the Tuning Advisor and/or SQL Server Profiler - both avaialble in the Performance Tools menu under your SQL Server folder in startup.

Also, learning the Management Studios reporting possibilities, and getting acquainted with execution plans are good starting points.

For the more advanced learners, start including statistics for query I/O (in Management Studios Query Window), using the Windows Performance Monitor to keep an eye on relevant SQL Server counters, etc. A good explanation to some of them can be found here: http://www.sql-server-performance.com/2005/sql-server-performance-monitor-coutners/

猫卆 2024-12-29 03:26:57

您能否表达您的疑问:

select * from b where b.aid = a.id and a.somefield = 'value'

Can you express your queries like:

select * from b where b.aid = a.id and a.somefield = 'value'
末蓝 2024-12-29 03:26:57

您还可以尝试使用exists 代替IN:s。如果有分配的数据可以提供帮助:

SELECT 
    *
FROM 
    a

SELECT 
    * 
FROM 
    b 
WHERE EXISTS
    (
        SELECT 
            NULL 
        FROM 
            a
        WHERE
            a.id=b.aid
    )

SELECT 
    * 
FROM 
    c 
WHERE EXISTS
    (
        SELECT 
            NULL 
        FROM 
            b
        WHERE
            c.bid=b.Id
            AND EXISTS
            (
                SELECT 
                    NULL 
                FROM
                    a
                WHERE
                    b.aid=a.id
            )
    )

You can also try using exists instead of IN:s. If there is allot of data that can help:

SELECT 
    *
FROM 
    a

SELECT 
    * 
FROM 
    b 
WHERE EXISTS
    (
        SELECT 
            NULL 
        FROM 
            a
        WHERE
            a.id=b.aid
    )

SELECT 
    * 
FROM 
    c 
WHERE EXISTS
    (
        SELECT 
            NULL 
        FROM 
            b
        WHERE
            c.bid=b.Id
            AND EXISTS
            (
                SELECT 
                    NULL 
                FROM
                    a
                WHERE
                    b.aid=a.id
            )
    )
素衣风尘叹 2024-12-29 03:26:57
  1. 避免使用*。明确提及列名称。
  2. 对于记录数量较多的表,使用非聚集索引。
  3. 使用联接而不是子查询。
  4. 在存储过程中使用“设置 NoCount On”。
  5. 您可以使用临时表。最后插入需要的数据并执行。不要忘记在退出存储过程之前删除它。
  1. Avoid usage of *. Mention explicitly the column names.
  2. Use Non Clustered Index for the tables with large number of records.
  3. Use Joins instead of Sub Queries.
  4. Use "Set NoCount On" in the Stored Procedures.
  5. You can use Temp tables. Insert the required data and execute it finally. Don;t forget to drop it before you exit the stored procedure.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文