Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6373349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:21:10+00:00 2026-05-25T01:21:10+00:00

I have a query that uses a lot of aggregate functions to build data

  • 0

I have a query that uses a lot of aggregate functions to build data for each item (a photo album). This data is critical and needs to be there. Currently the query alone takes about 1.5 seconds to execute, which is just too long.

I have created these functions because there are quite a few queries that all need to do these calculations, so it made sense to separate them into their own functions for ease of code maintainability. Otherwise, everytime a condition changes, there would be 5 or more places to go and look in to make the changes.

I would really like to know what I can do to make this query faster. It’s clear the aggregate functions are the culprit because if I replace them with a simple SELECT * it runs in about 0.1 seconds.

The only thing I can think of is removing the aggregate functions and doing inner joins / subqueries for each item that needs to be aggregated. As you can imagine, I would really like to NOT do this as it would require refactoring to loads of other places and make the code maintenance extremely messy.

Any suggestions? Thanks!

SELECT
fnAlbumGetNumberOfPhotos(albumId,1,0, 100) AS albumNumberOfPhotos,
fnLikeGetCount(1, 3, album.albumId) AS albumLikeCount,
fnGetAlbumPhotoViewCount(albumId) AS albumNumberOfPhotoViews,
fnLikeGetDoesUserLikeDislike(1, 3, album.albumId, 100) AS albumDoesUserLike,
fnObjectTagGetObjectTags(3, albumId) AS albumTags
FROM 
album
WHERE 0 = 0 
-- AND insert additional filters here
LIMIT 0,15

The functions are all simple aggregate functions, e.g:

CREATE DEFINER=`root`@`%` FUNCTION `fnAlbumGetNumberOfPhotos`(_albumId int, _photoIsActive tinyint, _photoIsDisabled tinyint, _userId int) RETURNS int(11)
BEGIN

  DECLARE outNumberOfPhotos int DEFAULT 0;
  DECLARE _userRoleId int DEFAULT 0;

  SET _userRoleId = (SELECT userRoleId FROM user WHERE userId = _userId);


  SET outNumberOfPhotos =
  (
    SELECT COUNT(*) AS outNumberOfPhotos
    FROM photo
    WHERE photoAlbumId = _albumId
    AND photoIsDisabled = _photoIsDisabled
    AND photoIsActive = _photoIsActive
    AND (photoPublishDate <= Now() || photoCSI = _userId || _userRoleId = 2)
  );

  RETURN outNumberOfPhotos;

END $$
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:21:11+00:00Added an answer on May 25, 2026 at 1:21 am

    When sql optimizer build execution plan for any query it splits query into singular operations and then manipulate with them. Unfortunately Function is a “black box” for sql optimizer – it doesn’t know what inside that function and assumes function call as a singular operation.
    If performance is critical for your query:

    1. recheck your indexes on tables
    2. INLINE your functions calls – in this case query optimizer can build
      an optimal execution plan base on your indexes
    3. instead of p.2 you can use VIEWs (of course if it’s possible) – they
      are transparent for optimizer.

    Hope this help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that uses a where clause. At times, this may be
I have this query that uses a UNION: $this->db->query(' SELECT DISTINCT users.user_pic, users.id, users.username,
I have a mysql query that uses union to join multiple queries into one
I have an Excel spreadsheet that uses a TFS query to pull information on
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I have a query that creates a result set like this: Rank Name 1
I have this query that returns the results by ordering it by focus.name ASC.
I have a query that looks like this SELECT * from myTable WHERE Date
I have this login form which uses AJAX to query a database for checking
We have a query that's taking 3 hours to finish. This wasn't a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.