I'm looking at a MS SQL Server database which was developed by a company that is an expert at database design (or so I'm told) and I noticed a curious pattern of JOINs/indexes. It's upside down from what I would have done, so I wonder if it has some performance benefits (the DB is fairly big).
The table structure (simplified pseudocode) is:
Table JOBS (about 1K rows):
job_id [int, primary key]
server_id [int, foreign key]
job_name [string]
Table JOB_HISTORY (about 17M rows):
history_id [int, primary key]
job_id [int, foreign key]
server_id [int, foreign key]
job_start [datetime]
job_duration [int]
Note the denormalization where the server_id is in both tables.
What they did is:
select
t1.job_name, t2.job_start, t2.job_duration
from
JOBS t1
inner join
JOB_HISTORY t2 on (t1.job_id = t2.job_id and t1.server_id = t2.server_id)
where
t1.server_id = #param_server_id
and t2.job_start >= #param_from
and t2.job_start <= #param_to
And they have indexes:
JOBS => (server_id)
JOB_HISTORY => (job_id, server_id, job_start)
In other words, when they select the rows, they first filter the jobs from JOBS table and then look up the relevant JOB_HISTORY entries. This is what the DB is forced to do, because of the indexes.
What I would have done it is the bottom-up version:
select
t1.job_name, t2.job_start, t2.job_duration
from
JOB_HISTORY t2
inner join
JOBS t1 on (t1.job_id = t2.job_id)
where
t2.server_id = #param_server_id
and t2.job_start >= #param_from
and t2.job_start <= #param_to
And a single index:
JOB_HISTORY => (server_id, job_start)
So, basically, I directly select the relevant rows from the large JOB_HISTORY and then just look for the attached data from the JOBS table.
Is there a reason to prefer one over the other?
Well, I was a bit bored so thought I'd re-create this for you. First setup (I'm using a numbers table to generate about 1K and 17M rows, of course, this is all random data and doesn't represent your system :) I'm also assuming theres a clustered index on each table, even though you imply you wouldn't have one.
USE TempDB;
GO
DROP TABLE IF EXISTS #Jobs;
DROP TABLE IF EXISTS #Job_History;
CREATE TABLE #Jobs
(
job_id INT IDENTITY PRIMARY KEY
,server_id INT
,job_name VARCHAR(50)
);
CREATE TABLE #Job_History
(
history_id INT IDENTITY PRIMARY KEY
,job_id INT
,server_id INT
,job_start DATETIME DEFAULT SYSDATETIME()
,job_duration INT DEFAULT ABS(CHECKSUM(NEWID())) % 5000
);
GO
INSERT INTO #Jobs
SELECT server_id = N.n
,job_name = CONVERT(VARCHAR(50), NEWID())
FROM DBA.Dim.Numbers N
WHERE n < 1000;
INSERT INTO #JOB_HISTORY
( job_id
,server_id
)
SELECT job_id = j1.job_id
,server_id = j1.server_id
FROM #Jobs j1
CROSS JOIN DBA.Dim.Numbers n
WHERE n < 17000;
Now, case 1 (their way)
DROP INDEX IF EXISTS Idx_Job_hist ON #Job_History;
CREATE NONCLUSTERED INDEX Idx_Job_Hist ON #Job_History (job_id, server_id, job_start);
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
DECLARE #param_server_id INT = 1234
DECLARE #param_from INT = 500
DECLARE #param_to INT = 1000
select
t1.job_name, t2.job_start, t2.job_duration
from
#JOBS t1
inner join
#JOB_HISTORY t2 on (t1.job_id = t2.job_id and t1.server_id = t2.server_id)
where
t1.server_id = #param_server_id
and t2.job_start >= #param_from
and t2.job_start <= #param_to;
And Case 2 (your way)
DROP INDEX IF EXISTS Idx_Job_hist ON #Job_History;
CREATE NONCLUSTERED INDEX Idx_Job_Hist ON #Job_History (server_id, job_start);
select
t1.job_name, t2.job_start, t2.job_duration
from
#JOB_HISTORY t2
inner join
#JOBS t1 on (t1.job_id = t2.job_id)
where
t2.server_id = #param_server_id
and t2.job_start >= #param_from
and t2.job_start <= #param_to;
And the (totally non-conclusive, because my system isn't your system...) results:
Their plan:
Your Plan:
The costs from your plan were much higher overall.
But then this is a rather artificial exercise to just prove the point - run the plans, the answer is - it depends.
(Thanks for the excuse to play with this, it was fun :)
The short answer here is that it doesn't really matter in what order you JOIN the tables. SQL is one of those languages where you tell the server what you want, not so much what you want it to do (**). (AKA a so-called declarative language).
The reason we are seeing different Query Plans for the two versions of your query is that they are not exactly the same. In the first one there is a requirement that server_id is identical in both tables, while in the second version this is no longer mentioned. t1.server_id can be anything there. If you re-add this requirement you'll notice that the query plans will be identical and that the server will do exactly the same thing 'under the hood' for either query.
FYI: Building on Les H's answer I took the liberty of checking what kind of index MSSQL would suggest here and not-surprisingly it came up with
CREATE NONCLUSTERED INDEX idx_test
ON [dbo].[Job_History] ([server_id],[job_start])
INCLUDE ([job_id],[job_duration])
FYI:
without the index, each query took about 1500ms to run
creating the index took about 20 seconds
with the index, each query takes about 200ms to run
(**: Yes, I'm aware that you can 'direct' what happens under the hood by means of HINTS, but experience shows that those should only be a last resort when the QO no longer is able to make sense of things. In most cases, when the statistics are up-to-date and the data layout is not extremely exotic, the Query Optimizer is ridiculously smart about finding the best way to get you the data you asked for.)
Related
Let's say I have the following two tables:
CREATE TABLE [dbo].[ActionTable]
(
[ActionID] [int] IDENTITY(1, 1) NOT FOR REPLICATION NOT NULL
,[ActionName] [varchar](80) NOT NULL
,[Description] [varchar](120) NOT NULL
,CONSTRAINT [PK_ActionTable] PRIMARY KEY CLUSTERED ([ActionID] ASC)
,CONSTRAINT [IX_ActionName] UNIQUE NONCLUSTERED ([ActionName] ASC)
)
GO
CREATE TABLE [dbo].[BigTimeSeriesTable]
(
[ID] [bigint] IDENTITY(1, 1) NOT FOR REPLICATION NOT NULL
,[TimeStamp] [datetime] NOT NULL
,[ActionID] [int] NOT NULL
,[Details] [varchar](max) NULL
,CONSTRAINT [PK_BigTimeSeriesTable] PRIMARY KEY NONCLUSTERED ([ID] ASC)
)
GO
ALTER TABLE [dbo].[BigTimeSeriesTable]
WITH CHECK ADD CONSTRAINT [FK_BigTimeSeriesTable_ActionTable] FOREIGN KEY ([ActionID]) REFERENCES [dbo].[ActionTable]([ActionID])
GO
CREATE CLUSTERED INDEX [IX_BigTimeSeriesTable] ON [dbo].[BigTimeSeriesTable] ([TimeStamp] ASC)
GO
CREATE NONCLUSTERED INDEX [IX_BigTimeSeriesTable_ActionID] ON [dbo].[BigTimeSeriesTable] ([ActionID] ASC)
GO
ActionTable has 1000 rows and BigTimeSeriesTable has millions of rows.
Now consider the following two queries:
Query A
SELECT *
FROM BigTimeSeriesTable
WHERE TimeStamp > DATEADD(DAY, -3, GETDATE())
AND ActionID IN (
SELECT ActionID
FROM ActionTable
WHERE ActionName LIKE '%action%'
)
Execution plan for query A
Query B
SELECT bts.*
FROM BigTimeSeriesTable bts
INNER JOIN ActionTable act ON act.ActionID = bts.ActionID
WHERE bts.TimeStamp > DATEADD(DAY, -3, GETDATE())
AND act.ActionName LIKE '%action%'
Execution plan for query B
Question: Why does query A have better performance than query B (sometimes 10 times better)? Shouldn't the query optimizer recognize that the two queries are exactly the same? Is there any way to provide hints that would improve the performance of the INNER JOIN?
Update: I changed the join to INNER MERGE JOIN and the performance greatly improved. See execution plan here. Interestingly when I try the merge join in the actual query I'm trying to run (which I cannot show here, confidential) it totally messes up the query optimizer and the query is super slow, not just relatively slow.
The execution plans you have supplied both have exactly the same basic strategy.
Join
There is a seek on ActionTable to find rows where ActionName starts with "generate" with a residual predicate on the ActionName LIKE '%action%'. The 7 matching rows are then used to build a hash table.
On the probe side there is a seek on TimeStamp > Scalar Operator(dateadd(day,(-3),getdate())) and matching rows are tested against the hash table to see if the rows should join.
There are two main differences which explain why the IN version executes quicker
IN
The IN version is executing in parallel. There are 4 concurrent threads working on the query execution - not just one.
Related to the parallelism this plan has a bitmap filter. It is able to use this bitmap to eliminate rows early. In the inner join plan 25,959,124 rows are passed to the probe side of the hash join, in the semi join plan the seek still reads 25.9 million rows but only 313 rows are passed out to be evaluated by the join. The remainder are eliminated early by applying the bitmap inside the seek.
It is not readily apparent why the INNER JOIN version does not execute in parallel. You could try adding the hint OPTION(USE HINT('ENABLE_PARALLEL_PLAN_PREFERENCE')) to see if you now get a plan which executes in parallel and contains the bitmap filter.
If you are able to change indexes then, given that the query only returns 309 rows for 7 distinct actions, you may well find that replacing IX_BigTimeSeriesTable_ActionID with a covering index with leading columns [ActionID], [TimeStamp] and then getting a nested loops plan with 7 seeks performs much better than your current queries.
CREATE NONCLUSTERED INDEX [IX_BigTimeSeriesTable_ActionID_TimeStamp]
ON [dbo].[BigTimeSeriesTable] ([ActionID], [TimeStamp])
INCLUDE ([Details], [ID])
Hopefully with that index in place your existing queries will just use it and you will see 7 seeks, each returning an average of 44 rows, to read and return only the exact 309 total required. If not you can try the below
SELECT CA.*
FROM ActionTable A
CROSS APPLY
(
SELECT *
FROM BigTimeSeriesTable B
WHERE B.ActionID = A.ActionID AND B.TimeStamp > DATEADD(DAY, -3, GETDATE())
) CA
WHERE A.ActionName LIKE '%action%'
I had some success using an index hint: WITH (INDEX(IX_BigTimeSeriesTable_ActionID))
However as the query changes, even slightly, this can totally hamstring the optimizer's ability to get the best query.
Therefore if you want to "materialize" a subquery in order to force it to execute earlier, your best bet as of February 2020 is to use a temp table.
For inner join there's no difference between filtering and joining
[Difference between filtering queries in JOIN and WHERE?
But here your codes create different cases
Query A: You are just filtering with 1000 record
Query B: You first join with millions of rows and then filter with 1000 records
So query A take less time than query B
I have a query that runs fairly fast under normal circumstances. But it is running very slow (at least 20 minutes in SSMS) due to how many values are in the filter.
Here's the generic version of it, and you can see that one part is filtering by over 8,000 values, making it run slow.
SELECT DISTINCT
column
FROM
table_a a
JOIN
table_b b ON (a.KEY = b.KEY)
WHERE
a.date BETWEEN #Start and #End
AND b.ID IN (... over 8,000 values)
AND b.place IN ( ... 20 values)
ORDER BY
a.column ASC
It's to the point where it's too slow to use in the production application.
Does anyone know how to fix this, or optimize the query?
To make a query fast, you need indexes.
You need a separate index for the following columns: a.KEY, b.KEY, a.date, b.ID, b.place.
As gotqn wrote before, if you put your 8000 items to a temp table, and inner join it, it will make the query even faster too, but without the index on the other part of the join it will be slow even then.
What you need is to put the filtering values in temporary table. Then use the table to apply filtering using INNER JOIN instead of WHERE IN. For example:
IF OBJECT_ID('tempdb..#FilterDataSource') IS NOT NULL
BEGIN;
DROP TABLE #FilterDataSource;
END;
CREATE TABLE #FilterDataSource
(
[ID] INT PRIMARY KEY
);
INSERT INTO #FilterDataSource ([ID])
-- you need to split values
SELECT DISTINCT column
FROM table_a a
INNER JOIN table_b b
ON (a.KEY = b.KEY)
INNER JOIN #FilterDataSource FS
ON b.id = FS.ID
WHERE a.date BETWEEN #Start and #End
AND b.place IN ( ... 20 values)
ORDER BY .column ASC;
Few important notes:
we are using temporary table in order to allow parallel execution plans to be used
if you have fast (for example CLR function) for spiting, you can join the function itself
it is not good to use IN with many values, the SQL Server is not able to build always the execution plan which may lead to time outs/internal error - you can find more information here
I have UI form which shows to user different aggregate information (fact, plan etc. - 6 different T-SQL queries run in parallel). Execution of pure SQL queries takes up to 3 seconds.
I use stored procedures with parameters, but there is no problem - call of SPs takes absolutely the same time.
Here I use example of one table and one query, another 5 queries and tables have the same structure. I use MS SQL Server 2012, it's possible to upgrade up to 2014 if any optimization reason.
Now I try to find all possible ways to improve it. And it should be only SQL ways.
Aggregate table structure:
create table dbo.plan_Total(
VersionId int not null,
WarehouseId int not null,
ChannelUnitId int not null,
ProductId] int not null,
Month date not null,
Volume float not null,
constraint PK_Total primary key clustered
(VersionId asc, WarehouseId asc, ChannelUnitId asc, ProductId asc, Month asc)) on PRIMARY
SP query structure:
ALTER PROCEDURE dbo.plan_GetTotals
#versionId INT,
#geoIds ID_LIST READONLY, -- lists from UI filters
#productIds ID_LIST READONLY,
#channelUnitIds ID_LIST READONLY
AS
begin
SELECT Id INTO #geos
FROM #geoIds
SELECT Id INTO #products
FROM #productIds
SELECT Id INTO #channels
FROM #channelUnitIds
CREATE CLUSTERED INDEX IDX_Geos ON #geos(Id)
CREATE CLUSTERED INDEX IDX_Products ON #products(Id)
CREATE CLUSTERED INDEX IDX_ChannelUnits ON #channels(Id)
SELECT Month, SUM(Volume) AS Volume
FROM plan_Total t
JOIN #geos g ON t.WarehouseId = g.Id
JOIN #products p ON t.ProductId = p.Id
JOIN #channels cu ON t.ChannelUnitId = cu.Id
WHERE VersionId = #versionId
GROUP BY Month
ORDER BY Month -- no any performance impact
END
Approx. execution time 600-800 ms. Time of another queries almost the same.
How can I dramatically decrease execution time? Is it possible?
What I've done already:
- Try columnstore indexes (clustered is not good because foreign key problem);
- Disable of non-clustered columnstore index is not solution, because in some tables need to update data online (user can change information);
- Rebuild all current indexes;
- Can't gather all tables in one.
Here is actual plan link:
Actual execution plan - for this plan i add real tables in joins instead of temp tables.
BR, thanks for any help!
Have you considered not asking not joining channel, product etc.?
At least channels - if you do not have 10.000 you can just load them "on demand" or "on application start" and cache them. This is a client side dictionary lookup.
Also Month, SUM(Volume)..... consider precalculating this, making a materialized view. Calculating this on demand is not what reporting should do and goes against data warehousing best practices.
All your solutions will not change that - they do not address the real problem: too much processing in the query.
See if this way works better
Create the TABLE type to have a PRIMARY KEY
Specify option RECOMPILE: force compiler to include cardinality of TABLE variables
Specify option OPTIMIZE FOR UNKNOWN: prevent parameter sniffing for #versionId
CREATE TYPE dbo.ID_LIST AS TABLE (
Id INT PRIMARY KEY
);
GO
CREATE PROCEDURE dbo.plan_GetTotals
#versionId INT,
#geoIds ID_LIST READONLY,
#productIds ID_LIST READONLY,
#channelUnitIds ID_LIST READONLY
AS
SELECT
Month,
SUM(Volume) AS Volume
FROM
plan_Total AS t
INNER JOIN #geoIds AS g ON g.Id=t.WarehouseId
INNER JOIN #productIds AS p ON p.Id=t.ProductId
INNER JOIN #channelUnitIds AS c ON c.Id=t.ChannelUnitId
WHERE
t.VersionId=#versionId
GROUP BY
Month
ORDER BY
Month
OPTION(RECOMPILE, OPTIMIZE FOR UNKNOWN);
GO
Ok, here I just show what I can find and how I increased speed of mu query.
List of addins:
Best way is to add Clustered columnstore index. For that you need to delete FK's but you can use triggers for example. This increase the query up to 3-4 times.
How you can see I use temp tables in query joins. I've changed one join (doesn't matter which) to IN operand like this "and t.productid in (select id from #productids)" it increased query pure speed twice.
This two made most impact to the query. Below I want to show the final query:
select [month], sum(volume) as volume
from #geos g
left join dbo.plan_Total t on t.warehouseid = g.id
join #channels cu on t.channelunitid = cu.id
where versionid = #versionid
and t.productid in (select id from #productids)
group by [month]
order by [Month]
With this changes I decrease query execution time from 0.8 to 0.2 ms.
the question is quite simple, but we've had so many issues with index/statistics updates not always resulting in the proper new execution plans in low-load environments that I need to check this with you guys here to be sure.
Say that you have the following tables:
/*
TABLES:
TABLE_A (PK_ID INT, [random columns], B_ID INT (INDEXED, and references TABLE_B.PK_ID))
TABLE_B (PK_ID INT, [random columns], C_ID INT (INDEXED, and references TABLE_C.PK_ID))
TABLE_C (PK_ID INT, [random columns])
*/
SELECT *
FROM TABLE_A A
JOIN TABLE_B B ON B.PK_ID = A.B_ID
JOIN TABLE_C C ON C.PK_ID = B.C_ID
WHERE A.randcolumn1 = 'asd' AND B.randcolumn2 <> 5
Now, since B is joined to A with its clustered PK column, shouldn't that mean that the index on B.C_ID will not be used as the information is already returned through the B.PK_ID clustered index? In fact, is it not true that the index on B.C_ID will never be used unless the query specifically targets the ID values on that index?
This may seem like a simple and even stupid question, but I want to make absolutely sure I'm getting this right. I'm thinking of making adjustments on our indexing, since we have a lot of unused indexes which have been inherited from an old datamodel and they're taking up quite a bit of space in a DB this size. And experience has shown that we cannot fully trust the execution plans on any environment apart from the production thanks to its extreme load compared to testing environments, which makes it difficult to test this out reliably.
Thanks!
The query optimizer is free to do as it pleases. It could execute the second join by scanning the C table, and for each row, looking up the matching row in B. The index you describe would help with that lookup.
SQL Server provides statistics to tell you if an index is actually used:
select db_name(ius.database_id) as Db
, object_name(ius.object_id) as [Table]
, max(ius.last_user_lookup) as LastLookup
, max(ius.last_user_scan) as LastScan
, max(ius.last_user_seek) as LastSeek
, max(ius.last_user_update) as LastUpdate
from sys.dm_db_index_usage_stats as ius
where ius.[database_id] = db_id()
and ius.[object_id] = object_id('YourTableName')
group by
ius.database_id
, ius.object_id
If the index isn't used for more than 2 months, it is usually safe to drop it.
I use Microsoft SQL Server 2008 (SP1, x64). I have two queries that do the same, or so I think, but they are have completely different query plans and performance.
Query 1:
SELECT c_pk
FROM table_c
WHERE c_b_id IN (SELECT b_id FROM table_b WHERE b_z = 1)
OR c_a_id IN (SELECT a_id FROM table_a WHERE a_z = 1)
Query 2:
SELECT c_pk
FROM table_c
LEFT JOIN (SELECT b_id FROM table_b WHERE b_z = 1) AS b ON c_b_id = b_id
LEFT JOIN (SELECT a_id FROM table_a WHERE a_z = 1) AS a ON c_a_id = a_id
WHERE b_id IS NOT NULL
OR a_id IS NOT NULL
Query 1 is fast as I would expect, whereas query 2 is very slow. The query plans look quite different.
I would like query 2 to be as fast as query 1. I have software that uses query 2, and I cannot change that into query 1. I can change the database.
Some questions:
why are the query plans different?
can I "teach" SQL Server somehow that query 2 is equal to query 1?
All tables have (clustered) primary keys and proper indexes on all columns:
CREATE TABLE table_a (
a_pk int NOT NULL PRIMARY KEY,
a_id int NOT NULL UNIQUE,
a_z int
)
GO
CREATE INDEX IX_table_a_z ON table_a (a_z)
GO
CREATE TABLE table_b (
b_pk int NOT NULL PRIMARY KEY,
b_id int NOT NULL UNIQUE,
b_z int
)
GO
CREATE INDEX IX_table_b_z ON table_b (b_z)
GO
CREATE TABLE table_c (
c_pk int NOT NULL PRIMARY KEY,
c_a_id int,
c_b_id int
)
GO
CREATE INDEX IX_table_c_a_id ON table_c (c_a_id)
GO
CREATE INDEX IX_table_c_b_id ON table_c (c_b_id)
GO
The tables are not modified after filling initially. I'm the only one querying them. They contains millions of records (table_a: 5M, table_b: 4M, table_c: 12M), but using only 1% gives similar results.
Edit: I tried adding FOREIGN KEYs for c_a_id and c_b_id, but that only made query 1 slower...
I hope someone can have a look at the query plans and explain the difference.
Join are slower, let me say by design. First query uses a sub-query (cacheable) to filter records so it'll produce less data (and less accesses to each table).
Did you read these:
http://www.sql-server-performance.com/2006/tuning-joins/
http://blogs.msdn.com/b/craigfr/archive/2006/12/04/semi-join-transformation.aspx
What I mean is that with IN the DB can do better optimizations like removing duplicates, stop at first match and similar (and these are from school memories so I'm sure it'll do much better). So I guess the question isn't why QP is different but how smart how deep optimizations can go.
You are comparing non equivalent queries also you are using left join in quite unusual way.
Generally if yours intention was to select all entries in table_c which has linked records either in table_a or table_b you should use exists statement:
SELECT c_pk
FROM table_c
WHERE Exists(
SELECT 1
FROM table_b
WHERE b_z = 1 and c_b_id = b_id
) OR Exists(
SELECT 1
FROM table_a
WHERE a_z = 1 and c_a_id = a_id
)
Since you can't change the query, at least you can improve the query's environment.
Highlight your query, right-click on it in SSMS and select "Analyze
Query in Database Engine Tuning Advisor."
Run the analysis to find out if you need any additional indexes or
statistics built.
Heed SQL Server's advice.