I’ll put an answer to complement,
Use SSD
It is interesting to check the cache plan of the queries, to know if this query is cached or not.
Otherwise I also recommend SSD, makes an absurd difference in reading the database when it has to be done reading the physical file. Apparently it’s a large database that sometimes makes it impossible to cache it in RAM.
-- View Adhocs (pre-stored queries when not stored)
use master
go
SELECT cast(text as varchar(8000)) as Query,(cp.size_in_bytes/1024) as KB
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE cp.cacheobjtype = 'Compiled Plan' AND cp.objtype = 'Adhoc' AND cp.usecounts = 1
order by KB desc
Check the consumption of IO
WITH Agg_IO_Stats
AS
(
SELECT
DB_NAME(database_id) AS database_name,
CAST(SUM(num_of_bytes_read + num_of_bytes_written) / 1048576.
AS DECIMAL(12, 2)) AS io_in_mb
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS DM_IO_Stats
GROUP BY database_id
)
SELECT
ROW_NUMBER() OVER(ORDER BY io_in_mb DESC) AS row_num,
database_name,
io_in_mb,
CAST(io_in_mb / SUM(io_in_mb) OVER() * 100
AS DECIMAL(5, 2)) AS Porcento
FROM Agg_IO_Stats
ORDER BY row_num;
Implementation plan
also check the SQL execution plan and look for indexes with bottlenecks.
In this case there is an index that is responsible for 100% of the consultation time. But in the case below there would not be much solution since "Clustered Index Seek" is the fastest type of index query.
Create new indexes
BEWARE, creating new indexes in varchar fields can cause an exponential increase in database size, especially in Databases with hundreds of thousands of records, just to give an example an index of mine in a field Varchar(100) occupies 5GB
Another detail to consider is whether the table suffers a lot of Sert, because every index causes a certain slowness in Sert and updates.
Defragment indexes
Check if the indexes exist if they are not fragmented, I usually use a script that defrags all indexes above 20% fragmentation.
Learn more: https://msdn.microsoft.com/pt-br/library/ms189858.aspx?f=255&MSPPError=-2147217396
Learn more: http://www.fabriciolima.net/blog/2011/02/16/monitorando-a-fragmentacao-dos-indices/
Code I use http://pastebin.com/iaFbCik8
Move this query to a Storedprocedure
This performance gain is not absurd, but the stored Procedure is compiled and this brings several benefits as an internal sql cache plan.
Codes that might be interesting to you
--Qtd. of times a query has been executed
SELECT text,plan_handle, cp.size_in_bytes,usecounts--,*
FROM sys.dm_Exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE text not like '%dm_exec_sql_text%' --para não aparecer essa propria query
and text not like '%dm_Exec_cached_plans%' --para não aparecer essa propria query
and text like '%select%' -- aqui coloca o começo da sua consulta
ORDER BY usecounts DESC
--Check missing index #### USE WITH EXTREME CARE!!!!!!
SELECT
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE NONCLUSTERED INDEX [SK01_'
+ OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) +']'+
' ON ' + dm_mid.statement+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns IS NOT NULL THEN ',' ELSE
'' END+ ISNULL (dm_mid.inequality_columns, '')
+ ')'+ ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement,dm_migs.user_seeks,dm_migs.user_scans
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
A very important thing not informed, what the DBMS and the version used , some DBMS have solutions for the type you need to do. In addition, optimization solutions (tuning) change from BD to BD. However in general they go through : use of indexes , collected statistics , analyze the plan of execution of queries.
– Motta
which database you use, Vinicius?
– Marllon Nasser
SQL Server @Marllonnasser
– vinibrsl
What version of SQL Server do you use? (beyond the year, state if it is standard, Enterprise, datacenter, etc....)
– Intruso
Search records using 'Like' or even use the index in the field (if it has). To properly use the text field index, switch to Starting with or relevant to your DBMS. But in this case, it will only return records that contain words started with the text you specify.
– Carlos Andrade
You have a maintenance routine for this bank, for example, compile indexes, update statistics?
– Ruberlei Cardoso