Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 01:26

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



SqlParameter causes slow MSSQL queries

Discussion on SqlParameter causes slow MSSQL queries within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
Mi4uric3's Avatar
 
elite*gold: 405
Join Date: Dec 2007
Posts: 6,615
Received Thanks: 6,356
SqlParameter causes slow MSSQL queries

Hi,

I'm currently creating a frontend for searching information in a MSSQL database and I'm facing the problem that it is so slow when I use a statement with placeholders and SqlParameter, that it runs into a timeout. Using the variable directly (string.Format, which is of course prone to SQL-Injection attacks) is fast, so I'm guessing it has to do with the SqlParameter.

The database has the following format and contains about 115.000.000 entries:
Code:
CREATE TABLE ExampleTable (
	ID INT NOT NULL,
	Name VARCHAR(128) NOT NULL DEFAULT '',
	Class CHAR(1) NOT NULL DEFAULT 'M',
	PRIMARY KEY (ID)
);
The tips I found while browsing the Internet basically consisted of those two things:
  1. Set ArithAbort ON
  2. Use explicit datatypes for the SqlParameter
So my code currently looks like this (simplified):

Code:
string searchName = "%SomeValue%";

SqlConnection connection = new SqlConnection(string.Format("Data Source={0};Database={1};Trusted_Connection=true", CONNECTION_SERVER, CONNECTION_DATABASE));
connection.Open();
new SqlCommand("SET ARITHABORT ON;", connection).ExecuteNonQuery();

using (SqlCommand command = new SqlCommand("SELECT TOP 100 ID, Name FROM dbo.ExampleTable WHERE Name LIKE [at]Name;", connection)) // Replace [at] with @, Elitepvpers replaces it with some [mention] tag
{
	command.Parameters.Add(new SqlParameter("Name", SqlDbType.VarChar, 128)).Value = searchName;
	List<ExampleClass> names = new List<ExampleClass>();
	using (SqlDataReader reader = command.ExecuteReader()) // This is where the timeout occurs.
	{
		while (reader.Read())
		{
			names.Add(new ExampleClass((int)reader[0], (string)reader[1]));
		}
	}
	// Do something with List<ExampleClass> names.
}
But if I do the following instead it is fast:
Code:
// ...
using (SqlCommand command = new SqlCommand(string.Format("SELECT TOP 100 ID, Name FROM dbo.ExampleTable WHERE Name LIKE '{0}';", searchName), connection))
// ...
I also tried setting all the other variables a SqlCommand can be constructed with with no change to the outcome.

Do you have any more tips what I can do?
Thanks in advance,
Mi4uric3
Mi4uric3 is offline  
Old 01/09/2017, 16:40   #2
 
XYZ-Cannon's Avatar
 
elite*gold: 0
Join Date: Jul 2011
Posts: 1,773
Received Thanks: 866
Use prepared SQL Statements or NHibernate as framework



XYZ-Cannon is offline  
Reply

Tags
arithabort, slow, sqlcommand, sqlparameter, varchar


Similar Threads Similar Threads
Upgraded MSSQL 2008 to SQL2012, now my modules load very slow
07/28/2016 - SRO Private Server - 0 Replies
Nvm fixed it:) #RequestClose!
[Release]Bunch of Useful Queries for MsSql Database Return Results
12/07/2015 - SRO PServer Guides & Releases - 4 Replies
- Finds Inventory Items Based on Char name and some additional info Use SRO_VT_SHARD_INIT Select CH.CharName16, INV.CharID, INV.ItemID, IT.OptLevel, IT.RefItemID, INV.Slot, REF.ReqGender, REFC.ReqLevel1, REFC.CodeName128 From _Inventory As INV Right Join _Items As IT On INV.ItemID = IT.ID64 Right Join _RefObjItem As REF On IT.RefItemID = REF.ID Right Join _RefObjCommon As REFC On REFC.ID = REF.ID Right Join _Char As CH On CH.CharID = INV.CharID Where CH.CharName16 = 'Pal2aDoX' And...
Gift/Slow buffen/nerfen UND Skill_Proto Slow einfügen?
10/03/2013 - Metin2 Private Server - 0 Replies
Hi there, hab da n paar fragen: Ich will in der Skill_proto bei z.B. Pfeilregen nen Slow einfügen. Ich finde da nur den "Attack_slow", bei MaSu's Geisterschlag gibt der ne bestimmte chance auf Slow. Ich will keine chance, sondern 100% Slow. Wie man die chance ändert weiß ich leider auch nicht. Dasselbe mit vergiften und Entzünden. Hat mir da jemand was? Dann wollte ich den effekt von Gift/Slow/Entzünden ändern...
[HOWTO] Logging ALL Queries on a MSSQL Database?
12/25/2010 - Dekaron Private Server - 8 Replies
Hi, after ALLOT of googling in found a very sweet function in mssql If you have "Profiler" install here is how you can track almost everything that is going on in the DB after creating the topic and result from http://www.elitepvpers.com/forum/dekaron-private-se rver/895715-test-response-time-db.html ive found out that it will take months (YES! months) to analyze all the data that is send from dekaronserver.exe so here is how you can trace it: Open SQL Server Profiler or Profiler



All times are GMT +2. The time now is 01:26.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.