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 6948431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:49:19+00:00 2026-05-27T13:49:19+00:00

I am running a select statement in a report that returns 30 rows in

  • 0

I am running a select statement in a report that returns 30 rows in 30 mS. When I add fields to the SELECT clause that are based on a custom function, the same query takes minutes to run.

The custom function takes three parameters – all from the main query – does its own COUNT() query then returns the integer result. The query in the custom function by itself runs in a 10mS, but when run from within the custom function, it takes 300mS.

Are custom functions compiled each time they are encountered in a query? What could be the reason for the slowness? In my Oracle days (15 years ago), I was used to creating custom functions, sticking them into queries, and they would zip through their actions. Are MySQL custom functions fundamentally different?

Edit:

I have just spotted this: if the custom function takes parameters passed to it from the query it is used in, then it is very slow. If I put in hard-coded parameter values, then it is very fast.

e.g.

SELECT my_func(foo.col1, foo.col2, foo.col2)
FROM foo;
-- 120 seconds for 30 rows

SELECT my_func(1, 2, 3)
FROM foo;
-- 10 milli seconds for 30 rows

I can SELECT my_func(x, y, z), where x, y and z are any combination of values that would be passed to it in the first query example, and the database barely blinks. I’m really scratching my head on this one.

The reason I need this function is to enable me to run a report in SugarCRM. The report writing plugin (KReports) gives me access to certain entities and relationships in the database, but there are a bunch of counts that the report does not allow access to. The custom function has been created to return the counts (ten of them) for each row the main report returns, effectively giving the user a 10xN value pivot table.

The report is only returning a handful of rows each time it returns, and that is what was fooling me. Those results are grouped from the underlying data, which is much larger. The custom function is being executed (I am assuming) on the main data set before the GROUP BY is applied, and so is being called up an order of magnitude more times than I thought.

If I had full control over the generation of the SQL in this report, I would select the 30 (groups) rows in an inner query, then wrap that in an outer query to call up the custom function on that smaller data set. It looks like I am going to have to use a different reporting tool to fix this.

  • 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-27T13:49:20+00:00Added an answer on May 27, 2026 at 1:49 pm

    My answer, in summary, is that the custom function was being called up 7000 times in a query and not 300 times as I originally thought. 300 times would be fine for this report, taking about 10 seconds. 7000 times takes over 3 minutes and is no good.

    The reason is that the custom function in the SELECT clause gets applied to all rows retrieved in the query before the GROUP BY clause is applied. The GROUP BY reduces the 700 rows retrieved to just 30 unique rows.

    I cannot change the way the query works in the reporting tool that I am using, so will move to a new reporting tool that provides more flexibility and more control over how the SQL is constructed.

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

Sidebar

Related Questions

I am trying to have a running average column in the SELECT statement based
I'm running a select that returns alphanumeric results, e.g: ABC-1 ABC-2 ABC-10 SAM-1 SAM-2
I am wondering if it is possible when running a MySQL select statement, that
I have a SQL Query running on SQL Server 2008 R2 that returns a
I tried running the following statement: INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION) SELECT (a.number,
I'm running this query: SELECT TOP 1 [DVD Copy].[Stock No] FROM [DVD Copy] WHERE
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
I'm currently running the following query: SELECT * from people WHERE id = 4;
I'm running the following query in Hypersonic DB (HSQLDB): SELECT (CASE foo WHEN 'a'
I'm running Mysql 5.0.77 and I'm pretty sure this query should work? SELECT *

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.