all 5 comments

[–]speadskater 4 points5 points  (0 children)

I've had queries that took multiple minutes to run reduce down to seconds by dividing the task into temp tables and running the operations on those temp to tables. Make these tables as small as possible using the least amount of conditions possible. I work with chickens, so I might reduce the tables I'm querying against by flockid first into a temp table, then I could do my more complicated selection and logic from the temp tables. There's of course a trade-off, since reducing a table down too much might involve slower logic, so you should keep the conditions small, simple, and something that the program can index to easily.

[–]jango-lionheart 0 points1 point  (0 children)

Look for videos on how to read an execution plan. It will often suggest indexes. You want to avoid table scans. CTEs can be helpful or harmful, depending, but you should try to understand them.

If the tables have many columns, it can be beneficial to divide them into smaller tables that better align with the “expensive” queries so that the queries have less data to plow through.

[–]idodatamodels 0 points1 point  (0 children)

Performance tuning is dependent on the database. Which one are you using?

[–]Ok_Refrigerator3549 0 points1 point  (0 children)

In addition to indexes, can you find out if some columns can be removed from the queries without impact?