It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

Database Query Optimization: How to Improve Query Performance in SQL

page: 1
1

log in

join
share:

posted on Sep, 12 2023 @ 03:26 AM
link   
I have an SQL database that stores a large amount of data, and I'm experiencing slow query performance when retrieving information. What are some strategies and best practices for optimizing database queries in SQL to achieve better performance?

Here's an example query I'm working with:


SELECT first_name, last_name, email
FROM users
WHERE registration_date >= '2023-01-01'
ORDER BY last_name ASC;


While this query works, it's becoming slow as the dataset grows. Could you provide guidance on how to optimize this query and potentially use indexes or other techniques to speed up the retrieval of data? Additionally, are there any common pitfalls I should be aware of when optimizing SQL queries?
Your insights, including code examples if applicable, would be greatly appreciated. Thank you!



posted on Sep, 12 2023 @ 04:51 AM
link   
a reply to: Mobo01

In general, bitwise 'does not equal' comparisons are faster than having to parse or convert record content for comparison, so, where possible see if you can structure queries that way.

Also, consider ways that classify sets of data in ways likely to be searched, so indexing can do its stuff.

Similarly, there are database schemas that can be far faster for particular search types. For instance, having some data in simple key-store format means that if you only have to search for data within a single simple table, instead of multiple columned data, things can be retrieved faster (of course, that depends a lot on the data).

There are also stacks of data optimization tools if your databases are becoming unweildy, too.

edit on 12-9-2023 by chr0naut because: (no reason given)



posted on Sep, 13 2023 @ 02:03 AM
link   
Asuming that you use Microsoft SQL server which is my specialty. I tuned a lot of databases for small companies and big internation corporations. Performance issues like these where queries become slower over time is allmost every time due to poor indexing and maintenance.

MS SQL server uses statistics to determin which algoritmes to use to collect the data. If a table gets altered allot with create,update and inserts, the statistics will become old. Old statistics could mean that SQL is using the wrong algoritmes to collect data which can cause slowdown. But also the indexes may become fragmented which also can cause slowdown

So the first thing you need to do is check if a maintenance plan, which defragments the indexes and updates the statistics, is implemented and running correctly.

The second problem is poor indexing. A good indexing strategy is key for good performance.
If you are using MS SQL server, you can use the execution plan of a query to see how SQL is collecting its data. If you seeing al lot of table scans, clustered index scans etc with high reads, than you know which column is causing the problem and could perform better with an index. But dont overdo it, because it could slow down inserts, updates and deletes.

Looking at your query, I would look at the registration_date column first. If no indexing is done, it could be doing do a tablescan to collect the data. A table scan is exactly what is says. It scans a table from top to bottom to collect the data it needs. And if a table grows in size over time, it would become slower and slower.

After al these things i would consider looking at the SQL server itself, how much memory does it have, on what kind of disks its running, how many cores does it have etc.
Sometimes you can fix it by adding more RAM, or (if its a virtual machine) add more cores. But most of the times, its just the maintenance and indexing which causes the problems.



posted on Sep, 13 2023 @ 02:45 AM
link   
Some extra information.
Clustered index scans and table scans are allmost everytime performance killers. You should try to avoid them as much as possible. Only on small tables its acceptable and a index could even harm performance.

You can see the execution plan of query by execution the query and press CTRL + L.
See: display an execution plan

You could also try to run the database tuning advisor to get an idea which indexes are missing for all the queries executed. Do not add all the adviced indexes because it will most likely make the issue much worse. Try to find indexes which make sense and are usable by multiple queries.

See database tuning advisor

Reminder, I do not know alot of how to tune other SQL vendors, but indexing is in many vendors are much of the same.

edit on 13-9-2023 by hoover1980 because: (no reason given)



posted on Sep, 13 2023 @ 06:21 AM
link   
a reply to: Mobo01

It looks like the registration_date field is a text field. I think things would be faster if the comparison was done on a numeric field, but for that you would need to convert the date to a numeric value, so instead of
WHERE registration_date >= '2023-01-01'
you would have something like
WHERE registration_date_num >= 44927


edit on 13/9/2023 by ArMaP because: (no reason given)



posted on Sep, 15 2023 @ 06:26 AM
link   
a reply to: Mobo01


we have fingerprint logs with literally millions of records.
since we need to access them in monthly basis (could be 16-prev month to 15-this month, or 21-prev month to 20 this month, etc based on customer payroll calc).
to index it we create a new record called payrolldate

--------------------------------
tdate ttime payrolldate employee_id machine_id code
2023-08-16 2023-09
2023-08-17 2023-09
2023-08-18 2023-09
...
2023-09-15 2023-09

and we have index by payrolldate and employee_id since every query is always by date and employee

select * from t_fingerlog where payrolldate = '2023-09' and employee_id = 99

and we also create a summary table, so we dont have to query for each employee if it's already done,
something like

t_fingerlogsums
employee_id payrolldate present absent late late_hours etc
99 2023-09 20 1 x x.xx


(post by milanjoy removed for a serious terms and conditions violation)

posted on Feb, 9 2024 @ 03:20 AM
link   
a reply to: Mobo01

test it with tablename (nolock)




top topics



 
1

log in

join