How do I optimize an SQL query in Moodle?

How do I optimize an SQL query in Moodle?

by sanjay sahani -
Number of replies: 7

The PostgreSQL logs for our Moodle database show that certain queries are exceeding one second in execution time. What strategies or tools can be used to identify and optimize these slow queries?

Average of ratings: -
In reply to sanjay sahani

How do I optimize an SQL query in Moodle?

by Tim Hunt -
Piksa bilong Core developers Piksa bilong Documentation writers Piksa bilong Particularly helpful Moodlers Piksa bilong Peer reviewers Piksa bilong Plugin developers
Well, whether a query taking 1 second is good or bad depends on the query.

If this is a bug complex report, that is probalby expected. For other queries it would be bad.

You have to look on a case-by-case basis. Find the query in the code, understand what is going on, then use Explain to work out if there are obvious ways to improve it. Then follow Moodle development processes to contribute any improvements.
Average of ratings:Useful (2)
In reply to sanjay sahani

How do I optimize an SQL query in Moodle?

by Howard Miller -
Piksa bilong Core developers Piksa bilong Documentation writers Piksa bilong Particularly helpful Moodlers Piksa bilong Peer reviewers Piksa bilong Plugin developers
This is rather like those generic security reports. They suggest somewhere to look next but don't guarantee that you have found a problem.
In reply to sanjay sahani

How do I optimize an SQL query in Moodle?

by AL Rachels -
Piksa bilong Core developers Piksa bilong Particularly helpful Moodlers Piksa bilong Plugin developers Piksa bilong Testers

You could try using the Ad-hoc database queries plugin check up on your queries. I've used it a couple of times to speed up a query. When you use it to run a query, at the bottom of the report it will tell you how many rows are in the report, the date and time the report was run, and how much time it took to run.

In reply to AL Rachels

How do I optimize an SQL query in Moodle?

by Tim Hunt -
Piksa bilong Core developers Piksa bilong Documentation writers Piksa bilong Particularly helpful Moodlers Piksa bilong Peer reviewers Piksa bilong Plugin developers
But, if you can, better to use 'real' DB tools where you can directly run queries, and use commands like EXPLAIN to see what is going on.
In reply to Tim Hunt

How do I optimize an SQL query in Moodle?

by AL Rachels -
Piksa bilong Core developers Piksa bilong Particularly helpful Moodlers Piksa bilong Plugin developers Piksa bilong Testers
Hi Tim,
Since I am literally at-the-moment, struggling along with a database query, would you mind telling me what you use as a 'real' DB tool?
In reply to AL Rachels

How do I optimize an SQL query in Moodle?

by Mark Sharp -
Piksa bilong Core developers Piksa bilong Particularly helpful Moodlers Piksa bilong Plugin developers

I use MySQL workbench which provides extra tools to manage my DB and save queries I don't need to have on Moodle itself. Here's a list of Postgres clients, many of which can work with any DB. Adhoc queries is limited because it prevents you doing some queries (DELETE, UPDATE, ALTER, EXPLAIN etc) and offers no intellisense.

And you'll want to use EXPLAIN because you'll discover if your query is using indices properly, or if you need to add indices (but not to core 😉)

In reply to Mark Sharp

How do I optimize an SQL query in Moodle?

by AL Rachels -
Piksa bilong Core developers Piksa bilong Particularly helpful Moodlers Piksa bilong Plugin developers Piksa bilong Testers
Thanks so much for the info Mark! I especially appreciate the Postgres clients list.