SIRS
SIRS

Reputation: 624

Is there any performance difference when executing SQL query straight from C# than querying the view

Let me clear the question with an example.

If I want to query a complex data gathered from different table in SQL Server View, that look something like this (assume that the view is very complex)

SELECT e.ID, e.etc s.etc
FROM TableE AS e 
LEFT OUTER JOIN TableS AS s ON e.ID = s.NIP
WHERE ID > 1000

Then on C# I query it like this:

new SqlCommand("SELECT * FROM VeryComplexView", connection)

IS the above way better than doing like this, without creating the view first:

var query = @"SELECT e.ID, e.etc s.etc
              FROM TableE AS e 
              LEFT OUTER JOIN TableS AS s ON e.ID = s.NIP
              WHERE ID > 1000";

new SqlCommand(query, connection)

The view is actually more complex than the example above.

Upvotes: 1

Views: 295

Answers (1)

Juozas
Juozas

Reputation: 935

To achieve better performance and more secure approach, please create stored procedure, and include yuor query in it. Then call your procedure from C# code.

  • procedure is pre-compiled structure(view - not), you can benefit from cache, etc.
  • in this way you can organize secure server rights chain: login - database user - database role - procedure

Upvotes: 1

Related Questions