Top "Query-optimization" questions

For questions about making database queries run faster.

Counting DISTINCT over multiple columns

Is there a better way of doing a query like this: SELECT COUNT(*) FROM (SELECT DISTINCT DocumentId, DocumentSessionId FROM DocumentOutputItems) …

sql sql-server performance tsql query-optimization
SQL: How to properly check if a record exists

While reading some SQL Tuning-related documentation, I found this: SELECT COUNT(*) : Counts the number of rows. Often is improperly used …

sql optimization query-optimization
How to find out what is locking my tables?

I have a SQL table that all of a sudden cannot return data unless I include with (nolock) on the …

sql sql-server sql-server-2008-r2 locking query-optimization
mysql select from n last rows

I have a table with index (autoincrement) and integer value. The table is millions of rows long. How can I …

mysql select query-optimization
Finding blocking/locking queries in MS SQL (mssql)

Using sys.dm_os_wait_stats I have identified what I believe is a locking problem wait type waittime pct …

sql-server sql-server-2005 sql-server-2008 query-optimization
JOIN queries vs multiple queries

Are JOIN queries faster than several queries? (You run your main query, and then you run many other SELECTs based …

mysql database join query-optimization
What is the optimal way to compare dates in Microsoft SQL server?

I have a SQL datetime field in a very large table. It's indexed and needs to be queried. The problem …

sql sql-server tsql datetime query-optimization
PostgreSQL - fetch the row which has the Max value for a column

I'm dealing with a Postgres table (called "lives") that contains records with columns for time_stamp, usr_id, transaction_id, …

sql postgresql query-optimization cbo cost-based-optimizer
How to do the Recursive SELECT query in MySQL?

I got a following table: col1 | col2 | col3 -----+------+------- 1 | a | 5 5 | d | 3 3 | k | 7 6 | o | 2 2 | 0 | 8 If a user searches for "1", …

mysql sql query-optimization recursive-query
Difference in MySQL JOIN vs LEFT JOIN

I have this cross-database query... SELECT `DM_Server`.`Jobs`.*, `DM_Server`.servers.Description AS server, digital_inventory.params, products.products_…

mysql query-optimization left-join inner-join