English 中文(简体)
两个表格的精度
原标题:Efficient LEFT JOIN on two tables

我正试图加入在服务器上的多个桌子,并保持快速业绩。

CREATE TABLE #tmpRecords 
(
    recordID INT NOT NULL, 
    filter1ID INT NOT NULL, 
    filter2ID INT NOT NULL, 
    filter3ID INT NULL, 
    filter4ID INT NULL, 
    filter5 VARCHAR(200) NOT NULL
)

CREATE NONCLUSTERED INDEX ix_tempRecords 
ON #tmpRecords (filter1ID, filter2ID, filter3ID, filter4ID, filter5);

INSERT INTO #tmpRecords 
VALUES (1, 1, 1, 1, 1,  test1 ),
       (1, 2, 1, 1, 1,  test1 ),
       (1, 1, 1, 1, 1,  test2 ),
       (1, 2, 1, 1, 1,  test2 ),
       (2, 1, 1, 1, 1,  test1 ),
       (2, 2, 1, 1, 1,  test1 ),
       (2, 1, 1, 1, 1,  test2 ),
       (2, 2, 1, 1, 1,  test2 ),
       (3, 1, 1, 1, 1,  test1 ),
       (4, 1, 1, 1, 1,  test1 ),
       (5, 1, 1, 1, 1,  test1 ),
       (6, 1, 1, 1, 1,  test1 ),
       (7, 1, 1, 1, 1,  test1 ),
       (8, 2, 1, 1, 1,  test1 ),
       (9, 3, 1, 1, 1,  test1 )

CREATE TABLE #tmpRecordsStock 
(
    filter1ID INT NULL, 
    filter2ID INT NULL, 
    filter3ID INT NULL, 
    filter4ID INT NULL, 
    filter5 VARCHAR(200) NULL, 
    StockQuantity INT NOT NULL
)

CREATE NONCLUSTERED INDEX ix_tempRecordsStock 
ON #tmpRecordsStock (filter1ID, filter2ID, filter3ID, filter4ID, filter5);

INSERT INTO #tmpRecordsStock 
VALUES 
(1,null,null,null,null,10),
(2,null,null,null, test1 ,15),
(null,null,null,null, test1 ,20),
(null,null,null,null, test2 ,30),
(null,1,null,null, test1 ,40),
(null,2,null,null, test1 ,50)

SELECT
    T.recordID, T.filter1ID, T.filter2ID, T.filter3ID, T.filter4ID, 
    MAX(T.StockQuantity) AS StockQuantity
FROM
    (SELECT
         r.recordID, r.filter1ID, r.filter2ID, r.filter3ID,  
         r.filter4ID, r.filter5, MIN(s.StockQuantity) AS StockQuantity
     FROM
         #tmpRecords r
     LEFT JOIN
         #tmpRecordsStock s ON ((s.filter1ID IS NULL OR s.filter1ID = r.filter1ID)
                                AND (s.filter2ID IS NULL OR s.filter2ID = r.filter2ID)
                                AND (s.filter3ID IS NULL OR s.filter3ID = COALESCE(r.filter3ID, 0))
                                AND (s.filter4ID IS NULL OR s.filter4ID = COALESCE(r.filter4ID, 0))
                                AND (s.filter5 IS NULL OR s.filter5 = r.filter5))
     GROUP BY
         r.recordID, r.filter1ID, r.filter2ID, r.filter3ID, 
         r.filter4ID, r.filter5) T
GROUP BY
    T.recordID, T.filter1ID, T.filter2ID, T.filter3ID, T.filter4ID

DROP TABLE #tmpRecords
DROP TABLE #tmpRecordsStock

说明如何以比上述更好的方式做到这一点。

我已经用真实的表格对指数进行了测试,并通过“条形表>。 而不是“条形” a

SELECT TOP 1 StockQuantity 
FROM #tmpRecordsStock 
ORDER BY StockQuantity ASC
问题回答

In my experience with derived tables, using the TOP keyword can improve performance.

    SELECT
    T.recordID, T.filter1ID, T.filter2ID, T.filter3ID, T.filter4ID, 
    MAX(T.StockQuantity) AS StockQuantity
FROM
    (SELECT TOP (2147483647)
         r.recordID, r.filter1ID, r.filter2ID, r.filter3ID,  
         r.filter4ID, r.filter5, MIN(s.StockQuantity) AS StockQuantity
     FROM
         #tmpRecords r
     LEFT JOIN
         #tmpRecordsStock s ON ((s.filter1ID IS NULL OR s.filter1ID = r.filter1ID)
                                AND (s.filter2ID IS NULL OR s.filter2ID = r.filter2ID)
                                AND (s.filter3ID IS NULL OR s.filter3ID = COALESCE(r.filter3ID, 0))
                                AND (s.filter4ID IS NULL OR s.filter4ID = COALESCE(r.filter4ID, 0))
                                AND (s.filter5 IS NULL OR s.filter5 = r.filter5))
     GROUP BY
         r.recordID, r.filter1ID, r.filter2ID, r.filter3ID, 
         r.filter4ID, r.filter5) T
GROUP BY
    T.recordID, T.filter1ID, T.filter2ID, T.filter3ID, T.filter4ID




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签