Questions tagged [query-performance]

For questions pertaining to the measurement or improvement of a SQL query (that deals with speed).

If you're asking "Why is my SQL query so slow?" or something like that, you need to do a little extra homework to get good answers from the Stack Overflow community.

What database?

Put this tag on your question along with the tag for your database: , . , or whatever database you use. Don't use more than one database tag, please. Optimization works somewhat differently from database to database.

How many rows and how fast?

Please figure out approximately how many rows you expect in your result set, and approximately how many rows are in each table involved with the query. Mention those numbers in your question. Please mention how long you hope your query needs to take, and how long it's taking. (That is, tell us what you mean by slow).

Don't oversimplify

Don't try to simplify your question by replacing your actual SELECT column lists with *. Using * in SELECT clauses can be harmful to query performance compared to specifying the list of columns you need.

Show your table definitions, with indexes

In your question include your table definitions and index definitions for each table and view involved in your query. Make sure your question shows all indexes in all relevant tables. When you're asking a performance question, please don't oversimplify your question by omitting indexes or columns you think might be irrelevant. The strangest things can be relevant to performance!

Show your database's explanation of how it satisfied your query

In your question please show the EXPLAIN PLAN output (it's just EXPLAIN in some table server makes and models) for your query.

Reference

Please be aware that query performance optimization can be an inexact science. This is a good reference. http://use-the-index-luke.com/

1977 questions
123
votes
3 answers

Select distinct values from a table field

I'm struggling getting my head around the Django's ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the following: SELECT DISTINCT myfieldname FROM mytable (or alternatively) SELECT…
alj
  • 2,291
  • 4
  • 24
  • 34
118
votes
9 answers

SQL Server IN vs. EXISTS Performance

I'm curious which of the following below would be more efficient? I've always been a bit cautious about using IN because I believe SQL Server turns the result set into a big IF statement. For a large result set, this could result in poor…
Randy Minder
  • 43,558
  • 44
  • 173
  • 312
85
votes
15 answers

Deleting millions of rows in MySQL

I recently found and fixed a bug in a site I was working on that resulted in millions of duplicate rows of data in a table that will be quite large even without them (still in the millions). I can easily find these duplicate rows and can run a…
Steven Surowiec
  • 9,196
  • 4
  • 30
  • 37
52
votes
3 answers

Why is query with phone = N'1234' slower than phone = '1234'?

I have a field which is a varchar(20) When this query is executed, it is fast (Uses index seek): SELECT * FROM [dbo].[phone] WHERE phone = '5554474477' But this one is slow (uses index scan). SELECT * FROM [dbo].[phone] WHERE phone =…
Greg Gum
  • 25,941
  • 27
  • 127
  • 194
45
votes
2 answers

Why is there a HUGE performance difference between temp table and subselect

This is a question about SQL Server 2008 R2 I'm not a DBA, by far. I'm a java developer, who has to write SQL from time to time. (mostly embedded in code). I want to know if I did something wrong here, and if so, what I can do to avoid it to happen…
Ward
  • 2,532
  • 1
  • 18
  • 37
42
votes
8 answers

Query times out when executed from web, but super-fast when executed from SSMS

I'm trying to debug the source of a SQL timeout in a web application that I maintain. I have the source code of the C# code behind, so I know exactly what code is running. I have debugged the application right down to the line that executes the…
Michael Bray
  • 14,338
  • 6
  • 39
  • 64
41
votes
8 answers

How do NULL values affect performance in a database search?

In our product we have a generic search engine, and trying to optimze the search performance. A lot of the tables used in the queries allow null values. Should we redesign our table to disallow null values for optimization or not? Our product runs…
36
votes
9 answers

How to improve performance on a clustered index seek

I'm trying to improve the performance on a query that is running very slowly. After going through the Actual Execution Plan; I found that a Clustered Index Seek was taking up 82%. Is there any way for me to improve the performance on an Index…
Abe Miessler
  • 75,910
  • 89
  • 276
  • 451
26
votes
2 answers

Does getting entities with AsNoTracking() disable the automatic call to DetectChanges()?

I've come to know this concept of AsNoTracking(), DetectChanges(), and AutoDetectChangesEnabled very recently. I understand that when fetching records from the database via Entity Framework with AsNoTracking() used, then Entity Framework does not…
Sayan Pal
  • 4,226
  • 4
  • 42
  • 72
25
votes
1 answer

SQLite: Should LIKE 'searchstr%' use an index?

I have a DB with several fields word_id — INTEGER PRIMARY_KEY word — TEXT ... ..and ~150k rows. Since this is a dictionary, I'm searching for a word with mask 'search_string%' using LIKE. It used to work just fine, taking 15ms to find matching…
nikans
  • 2,143
  • 25
  • 30
22
votes
4 answers

Entity Framework include poor performance

Context We appear to be having an Entity Framework 6.x related issue. We've spent weeks attempting to nail down performance issues and fixed most if not all which we can find/think of. In short, we are seeing a massive drop in performance when…
TVisser
  • 221
  • 2
  • 5
21
votes
2 answers

Conditional aggregation performance

Let us have the following data IF OBJECT_ID('dbo.LogTable', 'U') IS NOT NULL DROP TABLE dbo.LogTable SELECT TOP 100000 DATEADD(day, ( ABS(CHECKSUM(NEWID())) % 65530 ), 0) datesent INTO [LogTable] FROM sys.sysobjects CROSS JOIN…
Radim Bača
  • 10,193
  • 1
  • 15
  • 31
15
votes
5 answers

Loading multiple entities by id efficiently in Hibernate

So, I'm getting a number of instances of a particular entity by id: for(Integer songId:songGroup.getSongIds()) { session = HibernateUtil.getSession(); Song song = (Song) session.get(Song.class,id); processSong(song); } This generates a SQL…
Paul Taylor
  • 12,050
  • 34
  • 149
  • 295
13
votes
1 answer

MERGE JOIN on two indexes still causing a SORT?

This is a performance question simplified to join of two indexes. Take the following setup: CREATE TABLE ZZ_BASE AS SELECT dbms_random.random AS ID, DBMS_RANDOM.STRING('U',10) AS STR FROM DUAL CONNECT BY LEVEL <=1000000; CREATE INDEX ZZ_B_I ON…
Davor Josipovic
  • 4,418
  • 1
  • 31
  • 49
13
votes
6 answers

How to check if a particular mapping exists in a one-to-many mapping table

I am having a table that maintains the mapping of an EMPLOYEE_ID to the one or more ROLE_IDs that the employee can be assigned with. The ROLE_ID is a primary key of the ROLE table. Now, I am trying to find if a particular employee is a Team Leader…
Sarath Chandra
  • 1,804
  • 17
  • 36
1
2 3
99 100