Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This is my query. I want to reduce the query speed for this query. How to optimise that query?

SQL
Declare
	@Pivv NVarchar(Max),
	@Query NVarchar(Max),
	@Pivv1 NVarchar(Max),
	@Columns NVarchar(Max)

Select
	@Pivv = Coalesce(@Pivv + ',' , '') + 
		    Quotename(MatchType + '_' + '_Name'),
	@Pivv1 = Coalesce(@Pivv1 + ',' , '') + 
		     Quotename('HighestRunsAndWicketsIn' + MatchType),
	@Columns = Isnull(@Columns + ',', '') +
			   'Max(' + (MatchType + '_' + '_Name') + ') As ' + (MatchType + '_' + '_Name') +
			   ',' +
			   'Max(' + ('HighestRunsAndWicketsIn' + MatchType) + ') As ' + ('HighestRunsAndWicketsIn' + MatchType)
From
	PlayersCareerBatting t Join PlayersBioDataNew a
On
	t.Id=a.Id
Group By
	MatchType

Set @Query=
		'Select
			''Batting'',
			' + @Columns + '
		From (
				Select
					MatchType + ''_'' + ''_Name'' As Type1,
					FirstName + '' '' + MiddleName + '' '' + LastName As Runs1,
					''HighestRunsAndWicketsIn'' + MatchType As Type,
					RunsScored As Runs
				From ( 
						Select 
							FirstName, 
							MiddleName,
							LastName, 
							MatchType, 
							RunsScored
						From (
								Select 
									s.Id, 
									FirstName, 
									MiddleName,
									LastName, 
									MatchType, 
									RunsScored, 
									Row_Number() Over(Partition By MatchType Order By RunsScored Desc) rn
								From 
									PlayersCareerBatting s Join PlayersBioDataNew a 
								On s.Id=a.Id
							 ) x
						Where rn=1
					 ) x2
			 ) x3
		Pivot 
			(
				Max(Runs1) For Type1 In (' + @Pivv + ')
			) x4
		Pivot
			(
				Max(Runs) For Type In (' + @Pivv1 + ')
			) x5

		Union All

		Select
			''Bowling'',
			' + @Columns + '
		From (
				Select
					MatchType + ''_'' + ''_Name'' As Type1,
					FirstName + '' '' + MiddleName + '' '' + LastName As Wickets1,
					''HighestRunsAndWicketsIn'' + MatchType As Type,
					Wickets As Wickets
				From ( 
						Select 
							FirstName, 
							MiddleName,
							LastName,
							MatchType, 
							Wickets
						From (
								Select 
									s.Id, 
									FirstName, 
									MiddleName,
									LastName, 
									MatchType, 
									Wickets, 
									Row_Number() Over(Partition By MatchType Order By Wickets Desc) rn
								From 
									PlayersBowlingCareer s Join PlayersBioDataNew a 
								On s.Id=a.Id
							 ) x6
						Where rn=1
					 ) x7
			 ) x8 
		Pivot 
			(
				Max(Wickets1) For Type1 In (' + @Pivv + ')
			) x9
		Pivot
			(
				Max(Wickets) For Type In (' + @Pivv1 + ')
			) x10'

Exec(@Query)


Thanks in advance

What I have tried:

Actually I checked some points given in the internet but it doesn't use for me
Posted
Updated 26-Mar-18 11:40am
v2
Comments
Manfred Rudolf Bihy 26-Mar-18 9:53am    
Reducing query speed could be achieved by making the query more complex. Since you have not specified by what factor you'd like to reduce the speed it's kind of hard to say.
It may also be the case you just want to speed up your query, but you didn't ask for that. ;)
Maciej Los 26-Mar-18 13:44pm    
Reduce the number of subqueries...
CHill60 26-Mar-18 13:56pm    
A virtual 5!
Maciej Los 26-Mar-18 14:01pm    
Thank you, Caroline. I thought to post it as an answer, but...
Giri Bkm 26-Mar-18 21:16pm    
Oops! Sorry, this query is running slow right now. I want to run this query even more faster
Thanks in advance

1 solution

In a short: i'd reduce the number of subqueries!

For further details, please see:
Query Performance[^]
SQL Performance Tuning: Top 5 Ways to Find Slow Queries[^]
The Seven Sins against TSQL Performance - Simple Talk[^]
 
Share this answer
 
Comments
Giri Bkm 27-Mar-18 3:42am    
I'm very sorry. I don't want reduce. I want to increase the speed of that query. Pls give the solution for how can I increase the speed of that query. Thanks in advance
CHill60 27-Mar-18 4:37am    
To increase the speed of the query, reduce the number of sub-queries. Your query is far too complex.
The construction of the dynamic sql is also messy for us to follow without any data - replace that
Exec(@Query)
with
Print @Query
and share the final query.
Alternatively share some sample data and expected results and describe what you are actually trying to do
Maciej Los 27-Mar-18 5:47am    
Good advice!
Maciej Los 27-Mar-18 5:47am    
Seems, you don't understand... A number of sub-queries affects on query performance.
Giri Bkm 27-Mar-18 6:12am    
But i want that sub-queries, and also i dont know how to reduce the sub-queries

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900