How do I optimize an SQL query in Moodle?

How do I optimize an SQL query in Moodle?

av sanjay sahani -
Antall svar: 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?

Gjennomsnittlig vurdering: -
Som svar til sanjay sahani

How do I optimize an SQL query in Moodle?

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av 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.
Gjennomsnittlig vurdering:Useful (2)
Som svar til sanjay sahani

How do I optimize an SQL query in Moodle?

av Howard Miller -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av 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.
Som svar til sanjay sahani

How do I optimize an SQL query in Moodle?

av AL Rachels -
Bilde av Core developers Bilde av Particularly helpful Moodlers Bilde av Plugin developers Bilde av 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.

Som svar til AL Rachels

How do I optimize an SQL query in Moodle?

av Tim Hunt -
Bilde av Core developers Bilde av Documentation writers Bilde av Particularly helpful Moodlers Bilde av Peer reviewers Bilde av 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.
Som svar til Tim Hunt

How do I optimize an SQL query in Moodle?

av AL Rachels -
Bilde av Core developers Bilde av Particularly helpful Moodlers Bilde av Plugin developers Bilde av 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?
Som svar til AL Rachels

How do I optimize an SQL query in Moodle?

av Mark Sharp -
Bilde av Core developers Bilde av Particularly helpful Moodlers Bilde av 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 😉)