D
D
Daeamon2016-10-11 11:41:50
SQL
Daeamon, 2016-10-11 11:41:50

How to optimize MSSql query?

SELECT distinct
  [Vv0].[Deleted],
  [LtstRev].[LatestrevisionNo],
  [Vv1].[Name]      as 'Current Status',
  [Vv8].[ValueText] as 'Component name',
  [Vv10].[ValueCache] as 'Componet cache',
  [Vv2].[ValueText] as 'PartNumber',
  [Vv4].[ValueText] as 'FootPrint',
  [Vv5].[ValueText] as 'FootPrint path',
  [Vv6].[ValueText] as 'Library path',
  [Vv7].[ValueText] as 'Library ref',
  [Vv9].[ConfigurationName] as 'Config',
  [PrjID].[ProjectID] as 'ProjectID'
       FROM    (
              (
              (
              (
              (
              (
              (
              (
              (
              (
              (
              (
              [OTD_db].[dbo].[DocumentsA] As [CurStatID] 
              INNER JOIN [OTD_db].[dbo].[Status] AS [Vv1] ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueA] AS [Vv2] ON [Vv2].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[Variable] AS [Vv3] ON [Vv3].[VariableID] = [Vv2].[VariableID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueB] AS [Vv4] ON [Vv4].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueC] AS [Vv5] ON [Vv5].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueD] AS [Vv6] ON [Vv6].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueE] AS [Vv7] ON [Vv7].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueF] AS [Vv8] ON [Vv8].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[DocumentsB] AS [Vv0] ON [Vv0].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[DocumentConfiguration] AS [Vv9] ON [Vv9].[ConfigurationID] = [Vv8].[ConfigurationID]
              )
              INNER JOIN [OTD_db].[dbo].[Projects] AS [PrjID] ON [PrjID].[ProjectID] = [Vv8].[ProjectID]
              )
              INNER JOIN [OTD_db].[dbo].[VariableValueG] AS [Vv10] ON [Vv10].[DocumentID] = [CurStatID].[DocumentID]
              )
              INNER JOIN [OTD_db].[dbo].[DocumentsC] AS [LtstRev] ON [LtstRev].[DocumentID] = [Vv8].[DocumentID]
       WHERE
             [Vv1].[StatusID]   = (SELECT [V].[StatusID] from [OTD_db].[dbo].[Status] As [VA] where [VA].[Name] Like 'Шаблон') AND
             [Vv2].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VB] where [V]B.[VariableName] Like 'Условное_обозначение') AND ([Vv2].[ValueText] != '') AND
             [Vv4].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VC] where [V].C[VariableName] Like 'Footprint') AND ([Vv4].[ValueText] != '') AND
             [Vv5].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VD] where [V].[DVariableName] Like 'PcbLib') AND ([Vv5].[ValueText] != '') AND
             [Vv6].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VE] where [V].[VEariableName] Like 'SchLib') AND ([Vv6].[ValueText] != '') AND
             [Vv7].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VF] where [V].[VaFriableName] Like 'Library_ref') AND ([Vv7].[ValueText] != '') AND
             [Vv8].[VariableID] = (SELECT [V].[VariableID] from [OTD_db].[dbo].[Variable] as [VG] where [V].[VarGiableName] Like 'Наименование') AND ([Vv8].[ValueText] != 'Part') AND
             [Vv9].[ConfigurationID] = (SELECT [V].[ConfigurationID] from [OTD_db].[dbo].[DocumentConfiguration] as [VH] where [VH].[ConfigurationName] Like 'По умолчанию') AND
             [Vv10].[ValueCache] = [Vv8].[ValueCache] AND
             [Vv0].[Deleted] = 0 AND
             [LtstRev].[LatestRevisionNo] > 1
ORDER by [Vv2].[ValueText] ASC

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2016-10-11
@Sanasol

This is not M y SQL, but M S SQL.
I have almost the same request - as for me the best option. Through trial and error, I settled on this one.
With such a request, the least number of requests = speed of work.
If you start making additional data in the sampling cycle, it will work very slowly.
If it works very slowly, then you need to dig towards explain or something like that (I don’t know what it’s called for mssql).
And to optimize already structure of tables, indexes to create.

A
Alexey, 2016-10-11
@k1lex

I'll be honest. Your request is not something to understand, it is difficult to read it.
1. Divide the request into parts and use temps.
2. It is not necessary to put every JOIN in a subquery. Make it look like:

SELECT  
FROM TABLE
inner join  TABLE2 ON ...
inner join  TABLE3 ON ...

3. Why write a condition at the end when you can do it right in JOUN
SELECT *  FROM
 [OTD_db].[dbo].[DocumentsA] As [CurStatID]  
 INNER JOIN [OTD_db].[dbo].[Status] AS [Vv1] ON [Vv1].[StatusID] = [CurStatID].[CurrentStatusID] and [Vv1].[Name] Like 'Шаблон'

4. If there is a lot of data in the table and the query will work for a long time, then after specifying the table or its alias, add WITH (NOLOCK). This will release the lock on the table for the duration of the query.
5. Try not to use nested queries. type where FIELD in (select from TABLE). This greatly complicates the request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question