-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Public brains endpoint takes 12 seconds #1720
Comments
🤖 Hello @StanGirard! Good to see you here again. I hope you're doing well. Thank you for bringing this to our attention. It seems like the Looking at the function, it appears that the query is performing a count operation on the One potential optimization could be to calculate the Here's an example of how you could modify the function: CREATE OR REPLACE FUNCTION get_public_brain()
RETURNS TABLE
(
id INT,
name VARCHAR(255),
description TEXT,
last_update TIMESTAMP,
brain_type VARCHAR(255),
brain_count INT
)
AS $$
BEGIN
RETURN QUERY
WITH brain_counts AS (
SELECT
bu.brain_id,
COUNT(*) as brain_count
FROM
brains_users bu
GROUP BY
bu.brain_id
)
SELECT
b.brain_id as id,
b.name,
b.description,
b.last_update,
b.brain_type,
a.*,
bc.brain_count
FROM
brains b
LEFT JOIN api_brain_definition a ON b.brain_id = a.brain_id
LEFT JOIN brain_counts bc ON b.brain_id = bc.brain_id
WHERE
b.status = 'public'
ORDER BY
bc.brain_count DESC;
END; $$
LANGUAGE plpgsql; This modification calculates the Please note that this is just a suggestion and the actual performance improvement may vary depending on the specific characteristics of your data and database server. It would be beneficial to test this modification in a development environment before deploying it to production. I hope this helps! If you have any further questions or need additional assistance, please don't hesitate to ask. Sources |
What happened?
CREATE OR REPLACE FUNCTION get_public_brain()
RETURNS TABLE
(
id INT,
name VARCHAR(255),
description TEXT,
last_update TIMESTAMP,
brain_type VARCHAR(255),
brain_count INT
)
AS $$
BEGIN
RETURN QUERY
SELECT
b.brain_id as id,
b.name,
b.description,
b.last_update,
b.brain_type,
a.,
(SELECT COUNT() FROM brains_users bu WHERE bu.brain_id = b.brain_id) as brain_count
FROM
brains b
LEFT JOIN api_brain_definition a ON b.brain_id = a.brain_id
WHERE
b.status = 'public'
ORDER BY
brain_count DESC;
END; $$
LANGUAGE plpgsql;
Relevant log output
Twitter / LinkedIn details
No response
The text was updated successfully, but these errors were encountered: