What is the most efficient way to get the sql query from EF? I need this so I can run the query analysis and find its execution plan.
I know I can hook profiler to SQL server, but this step is a pain and a tremendous hit on productivity, almost enough to give up ORM altogether.
Is there a better, more efficient way to optimize EF queries?
The ToTraceString method can do this for you.
If you cast your IQueryable to a ObjectQuery, you can use the ToTraceString()-method to see the SQL query.
Is there a better, more efficient way to optimize EF queries?
Yes, buy EF Profiler or Huagati Profiler. Alternatively use EF Provider wrapper for tracing.
I know I can hook profiler to SQL server, but this step is a pain and
a tremendous hit on productivity, almost enough to give up ORM
altogether.
Profiler is only half of the story. There is also Database Engine Tuning Advisor and these tools are the main tool set if you really want to optimize SQL queries - but optimizing SQL queries you don't have under your control is very hard and sometimes impossible.
Related
Background: I have recently just gotten into optimising my SQL queries to get better performance. Previously, I am using ASP.NET Core EF Linq-to-SQL to quickly prototype. Things work correctly, but are obviously not optimised.
I figured I start by looking at the SQLs generated and running them in SQL Management Studio and looking at the actual execution plans of each query.
I know how to read a plan in general, what each tree/node means. Things I'm not sure about I can google relatively easily.
I'm wondering for those query tuning experts out there, are there any experiences you can share which will help me identify why a query runs slow?
For example, looking at a JOIN in the execution plan and finding it is not looking up an index is one of the simplest fixes (the only one I can identify right now =D).
What else should I be looking out for?
I have a question. I have used ADO and Entity Framework. No doubt, Entity Framework facilitates us for writing queries in c# so we can also debug these queries. Till now what I have heard is that query processing is more fast if we execute them in SQL. So I just want to know that processing queries in c# could be slower than processing them in SQL? What I want to say is that query written in Entity framework would be slow/fast than query written in SQL?
I hope I am clear.
Please suggest.
SQL will be faster but by how much depends on your queries. So there is no definite answer as you will have to test the results for yourself.
In my experience the performance of the entity framework is generally acceptable. If 95% of your queries are fast enough in EF then you can always write the remaining slow 5% in SQL.
There are similar thread here: Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?
I this thread will help you make decision what are good/bad of both.
I'm looking for a tool, which would help creating complex SQL queries. Sometimes it's difficult to even verify, whether the results of a query are correct. It's especially easy to get queries joining several tables to return too little or too much data.
The tool should enable at least creation of test tables, some kind of visualization how the queries gather their data and hopefully give better parsing of error cases than for example Oracle does.
Are there tools like this or do I have to stick with creating test tables manually, filling them with test data and commiting all kinds of queries with SQuirrel SQL?
When you have a very complex query it is usually easiest to validate by breaking it up into multiple queries that populate temp tables. These intermediary results can be individually verified and then you bring them together to produce the final result set. Depending on performance needs you can stick with the temp table approach or you can then rewrite to a single statement. Typically when I have a huge query it is for background processing so I stick with the temp table approach.
What RDBMS are you using? All of the major ones have some type of console available (e.g.-SSMS in SQL Server, Toad in Oracle, MySQL Query Browser/Administrator for MySQL, etc.), and they all have Query Execution Plans where you can see how the query will actually run. So, the answer to your question is that it's entirely dependent on what RDBMS you're using, but the safe bet answer is: Yes.
I recommend trying SQL Server 2008 Management Studio Express (SSMSE) if you are working with SQL Server. I have used it at work and I believe it does everything you are looking for.
You can get it and SQL Server (express editions) here.
Certainly not a free, open-source solution, but I believe Quest Software's TOAD will fit your requirements. Quest seems to offer alot of tools in that space...they have tools for modeling and analysis, however I've never used the modeler or analyzer.
I personally have experience with the commercial version of TOAD for Oracle. It's GUI is overwhelming at first, but after you mentally filter out all of the extra buttons that you'll never use, it's manageable.
In the never-ending search for performance (and my own bludgeoning experience), I've learnt a few things that could drag down the performance of a SQL statement.
Obsessive Compulsive Subqueries Disorder
Doing crazy type conversions (and nest those into oblivion)
Group By on aggregate functions of said crazy type conversions
Where fldID in (select EVERYTHING from my 5mil record table)
I typically work with MSSQL. What tools are available to test the performance of a SQL statement? Are these tools built in and specific to each type of DB server? Or are there general tools available?
SQL Profiler (built-in): Monitoring with SQL Profiler
SQL Benchmark Pro (Commercial)
SQL Server 2008 has the new Data Collector
SQL Server 2005 (onwards) has a missing indexes Dynamic Management View (DMV) which can be quite useful (but only for query plans currently in the plan cache): About the Missing Indexes Feature.
There is also the SQL Server Database Engine Tuning Advisor which does a reasonable job (just don't implement everything it suggests!)
I mostly just use Profiler and the execution plan viewer
Execution Plans are one of the first things to look at when debugging query performance problems. An execution plan will tell you how much time is roughly spent in each portion of your query, and can be used to quickly identify if you are missing indexes or have expensive joins or loops.
MSSQL has a database tuning advisor that will often recommend indexes for tables based upon common queries run during the tuning period, however it wo't rewrite a query for you.
In my opinion, experience and experimentation are the best tools for writing good SQL queries.
In mysql (may be in other databases too) you can EXPLAIN your query to see what database server thinks about it. This usually used to deside which indexes should be created. And this one is build-in, so you can use it without installing additional software.
Adam Machanic has a simple tool called SqlQueryStress that might be of use. It is designed to be used to "run a quick performance test against a single query, in order to test ideas or validate changes".
Are there any standard queries that can be run that will show the performance of a SQL Server 2005 database?
Note: I need to know the performance of every aspect of the database.
EDIT:
I am looking for a way to measure the time it takes for typical queries to execute. I am then going to apply indexing to certain tables in the database and then time how long the same queries take to execute and see if there is a significant difference. Is there an easy way to do this?
Thanks!
(Edited, link hopefully fixed)
For general background research/analysis of SQL Server performance, I prefer to watch how SQL is performing as it is performing. The best tools for that are SQL Profiler and sometimes Windows System Monitor (aka Performance Monitor aka PerfMon). Alas, neither are particularly simple, let alone simple queries against the system -- though some PerfMon counters are exposed through a few DMViews I can't dig up just now.
BOL has reasonable information on these; a good top-level (online) page for this is here. Be wary, there is serious DBA stuff beyond that point
There are some dynamic management views and functions build in:
http://msdn.microsoft.com/en-us/library/ms188754(SQL.90).aspx
select * from sys.dm_db_index_usage_stats
select * from sys.dm_os_memory_objects