简体   繁体   中英

Speed up MsSQL query

I have a very huge database, 3k+ rows

It takes 4 whole seconds to load the page, with 2 big querys How to speed it up?

First:

SELECT Description
FROM ComputerIdentity JOIN
     MakeModelIdentity 
     ON ComputerIdentity.MakeModelID = MakeModelIdentity.ID
WHERE MakeModelIdentity.DeviceName='Laptop' AND
      ComputerIdentity.Description LIKE '%uitleen%' AND
      MakeModelIdentity.DeviceName = 'Laptop' AND
      MakeModelIdentity.Model = 'Chromebook' 
ORDER BY ComputerIdentity.ID ASC

Second:

SELECT Inlognaam,VolledigeNaam FROM PersoneelEnLeerlingen

For this query (which I've "simplified" using table aliases and removing the redundant WHERE condition):

SELECT ci.Description
FROM ComputerIdentity ci JOIN
     MakeModelIdentity mmi
     ON ci.MakeModelID = mmi.ID
WHERE mmi.DeviceName = 'Laptop' AND
      ci.Description LIKE '%uitleen%' AND
      mmi.Model = 'Chromebook' 
ORDER BY ci.ID ASC;

You can speed the query with indexes on MakeModelIdentity(DeviceName, Model, ID) and ComputerIdentity(MakeModelID, Description, ID) .

The second query is really simple and the speed only depends on the size of the data.

3K rows is miniscule, SQL Server Engine doesn't need your help to optimize it. Still here are some suggestions from the book of optimization. For First :

SELECT ci.Description
FROM ComputerIdentity ci JOIN
     MakeModelIdentity mmi
     ON ci.MakeModelID = mmi.ID
WHERE mmi.DeviceName = 'Laptop'  AND
      mmi.Model = 'Chromebook' AND
      ci.Description LIKE '%uitleen%' --reordered
ORDER BY ci.ID ASC;
  1. Make sure your IDs are integer
  2. Create clustered indexes on ComputerIdentity.MakeModelID and MakeModelIdentity.ID, in case you can make them primary key, that would be the best. Create a non-clustered index on Description, Device Name and Model
  3. If you can remove '%' operator try to that. '%' operator in front is very costly, by which I mean '%uitleen%' is slower than 'uitleen%'. If you can remove like operator, that would be great.

For Second: Create non-clustered indexes on the columns you are selecting, which doesn't have a clustered index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM