是否应该使用临时表在存储过程之间传递数据?

发布于 2024-08-11 07:32:24 字数 851 浏览 4 评论 0原文

我有许多搜索函数(存储过程),它们需要返回具有完全相同列的结果。

这是采用的方法:

每个存储过程都有一般结构:

CREATE TABLE #searchTmp (CustomerID uniqueIdentifier)

INSERT INTO #searchTmp
SELECT C.CustomerID FROM /**** do actual search here, based
                              on stored proc arguments ****/

EXEC spSearchResults

DROP TABLE #searchTmp

在上面,spSearchResults 在选择中使用#searchTmp 表。 spSearchResults 总是返回一个具有相同列的表,并且有相当多的连接。

但是,与使用临时表相比,以下方法更可接受:

SELECT col1, col2, col3, col4, ....等等,很多列... FROM table1 LEFT JOIN table 2 ON ...等等,很多连接... 在哪里...在这里进行实际搜索...

如果需要进行 10 次不同的搜索(例如,根据邮政编码搜索客户、根据姓氏搜索一次等),则第二种方法意味着存在大量重复的列和指定的联接。如果使用搜索函数的代码发生更改,从而需要返回新列,则需要更新 10 个存储过程。

我完全赞成第一种方法,但我只是想知道第二种方法有什么好处。表现?

或者还有第三种方法?

I have a number of search functions (stored procedures) which need to return results with exactly the same columns.

This is the approach that was taken:

Each stored procedure had the general structure:


CREATE TABLE #searchTmp (CustomerID uniqueIdentifier)

INSERT INTO #searchTmp
SELECT C.CustomerID FROM /**** do actual search here, based
                              on stored proc arguments ****/

EXEC spSearchResults

DROP TABLE #searchTmp

In the above, spSearchResults use the #searchTmp table in a select. spSearchResults would always return a table with the same columns, and had quite a few joins.

However, rather than use a temporary table, is the following approach more acceptable:


SELECT col1, col2, col3, col4, .... etc, lots of columns ...
FROM table1 LEFT JOIN table 2 ON ... etc, lots of joins ...
WHERE ... DO ACTUAL SEARCH HERE ...

If there are 10 different searches to do (eg. search for a customer based on postcode, one search based on surname etc.), this second approach means there is lots of duplication of columns and joins specified. If the code that uses the search functions changes such that a new column is needed to be returned, there's then 10 stored procedures that need to be updated.

I'm all in favour of the first method, but i just wondered what benefits the second method gives. Performance?

Or is there a third method?

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

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

发布评论

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

评论(3

胡渣熟男 2024-08-18 07:32:24

只要临时表内容代表要输出的最终唯一键集,并且之后不再对结果集进行进一步的修剪,那么它就是实现您的要求的一种非常有效的方法。

仅当临时表包含一组中间键且在后续查询中进一步减少时才会出现问题。在这种情况下,在一个查询中读取所有数据会更加高效。

编辑:正如 Mark 所说,由于查询优化器能够在单个查询方法中使用多个线程,但不能在临时表方法中使用多个线程,因此可能会存在性能差异。您必须权衡这一潜在收益与临时表方法可维护性的巨大改进,并决定哪一个对您更重要。与通常的数据库问题一样,最好测量性能而不是猜测性能。

As long as the temp table contents represent the final set of unique keys to be output, and no further trimming of the resultset is done afterwards, then it is a very efficient way of implementing your requirements.

Problems will only arise if the temp table contains an intermediate set of keys which is cut down further in subsequent queries. In that scenario, reading all the data in one query will be more efficient.

EDIT: As Mark says, there may be a performance difference due to the query optimiser being able to use multiple threads in the single query approach, but not in the temp table approach. You have to weigh this potential gain against the vast improvement in maintainability of the temp table approach, and decide which is more important for you. As usual with database questions, it's better to measure performance rather than guess at it.

喜爱皱眉﹌ 2024-08-18 07:32:24

第二个应该表现更好。它将是您想要返回的数据的较小部分。

第二个使查询优化器有机会按照它想要的任何顺序执行查询,第一个强制首先从第一个表中进行选择(在您的情况下可能是想要的)。您还可以在第二个,即多个线程处理查询,因为优化器可以执行此操作。

要检查此情况,请运行 Showplan(Sybase 或 SQL Server)或 EXPLAIN (Iracle) 等以查看生成的实际查询。

The second should perform better. It will the smaller proportion of the data that you want to return.

The second gives the query optimiser the chance to do the query in any order it wants, the first forces a select from the first table first (which in your case is likely to be the one wanted).You can also use parallel queries on the second one ie more than one thread working on the query as the optimiser can do this.

To check on this run a Showplan (Sybase or SQL Server) or EXPLAIN (Iracle) etc to see the actual query generated.

赢得她心 2024-08-18 07:32:24

如果使用 Microsoft Sql Server,我更喜欢第三种方法:

Create Function GetSearchKeys([Search parameters here])
Returns @Keys Table (pk Int Primary Key Not Null)
As
Begin
      Insert @Keys(pk)
      Select C.CustomerID 
      From /**** do actual search here, based 
               on Search parameters ****/
      Return
End

  -- ----------------------------------------------------

然后,在每个存储过程中,

  SELECT col1, col2, col3, col4, .... etc, lots of columns ... 
  FROM table1 
     LEFT JOIN table 2 
        ON ... etc, lots of joins
     Join schema.GetSearchKeys([Search parameters here]) K
        on K.pk = [whatever table.column has the primary key in  it]

If using Microsoft Sql Server, I prefer a third method:

Create Function GetSearchKeys([Search parameters here])
Returns @Keys Table (pk Int Primary Key Not Null)
As
Begin
      Insert @Keys(pk)
      Select C.CustomerID 
      From /**** do actual search here, based 
               on Search parameters ****/
      Return
End

  -- ----------------------------------------------------

And then, in each stored proc,

  SELECT col1, col2, col3, col4, .... etc, lots of columns ... 
  FROM table1 
     LEFT JOIN table 2 
        ON ... etc, lots of joins
     Join schema.GetSearchKeys([Search parameters here]) K
        on K.pk = [whatever table.column has the primary key in  it]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文