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

  • Home
  • SEARCH
  • 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 8429567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:22:26+00:00 2026-06-10T05:22:26+00:00

i made a query for sql but it’s really heavy and i really don’t

  • 0

i made a query for sql but it’s really heavy and i really don’t see how i can optimize it, the explanations comes after :

SELECT tyds.user_tyd, 
       tyds.product_tyd, 
       tyds.action_tyd, 
       products.price_product, 
       data_8, 
       data_9, 
       avg_8, 
       sum_9 
FROM   tyds 
       INNER JOIN products 
               ON tyds.product_tyd = products.id_product 
       INNER JOIN (SELECT product_tyd, 
                          Avg(data_tyd) AS avg_8 
                   FROM   tyds 
                   WHERE  action_tyd = 8 
                   GROUP  BY product_tyd) Agg_1 
               ON Agg_1.product_tyd = tyds.product_tyd 
       INNER JOIN (SELECT product_tyd, 
                          Sum(data_tyd) AS sum_9 
                   FROM   tyds 
                   WHERE  action_tyd = 9 
                   GROUP  BY product_tyd) Agg_2 
               ON Agg_2.product_tyd = tyds.product_tyd 
       INNER JOIN (SELECT product_tyd, 
                          data_tyd AS data_8 
                   FROM   tyds 
                   WHERE  user_tyd = 3 
                          AND action_tyd = 8) Agg_3 
               ON Agg_3.product_tyd = tyds.product_tyd 
       INNER JOIN (SELECT product_tyd, 
                          data_tyd AS data_9 
                   FROM   tyds 
                   WHERE  user_tyd = 3 
                          AND action_tyd = 9) Agg_4 
               ON Agg_4.product_tyd = tyds.product_tyd 
WHERE  tyds.user_tyd = 3 
       AND tyds.action_tyd = 1 
GROUP  BY tyds.product_tyd 

All this jointures because i need to get a lot of things :
I define an id_user in tyds.user_tyd = 3
tyds.action_tyd = 1 because i want to group by the tyds.product_tyd WHERE the previous id_user as an tyds.action_tyd = 1.

Then i want :
-data_8 is the value of tyds.data_tyd WHERE user_tyd = 3 AND action_tyd = 8
-same for data_9

-for AVG and COUNT i want to skip the tyds.user_tyd condition and only group by id_product.

Actually, this query is working but i think it’s really heavy and has too much SELECT…

I put a similar question a month ago but i had to review my architecture so i’m sorry…

Thanks a lot.

e1 : i use MySQL

  • 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-06-10T05:22:28+00:00Added an answer on June 10, 2026 at 5:22 am

    You don’t actually need all of those joins and subqueries; you can eliminate most of them by moving logic from WHERE-clauses into the field-list, using CASE expressions:

    SELECT 3 AS user_tyd, tyds.product_tyd, 1 AS action_tyd,
           products.price_product,
           MAX(CASE WHEN user_tyd = 3 AND action_tyd = 8 THEN data_tyd END) data_8,
           MAX(CASE WHEN user_tyd = 3 AND action_tyd = 9 THEN data_tyd END) data_9,
           AVG(CASE WHEN action_tyd = 8 THEN data_tyd END) AS avg_8,
           SUM(CASE WHEN action_tyd = 9 THEN data_tyd END) AS sum_9
      FROM tyds
      JOIN products
        ON tyds.product_tyd = products.id_product
     WHERE tyds.product_tyd IN
             ( SELECT product_tyd
                 FROM tyds
                WHERE tyds.user_tyd = 3
                  AND tyds.action_tyd = 1
             )
     GROUP
        BY tyds.product_tyd
    ;
    

    (Note: the MAX(...) in the third and fourth lines are just a way of forcing a non-null value to be preferred over a null one.)

    You’ll have to test to see if this is actually faster, but I’m betting that it is.

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

Sidebar

Related Questions

I have made an SQL query from two tables. Everything works good but problem
In my model I've made this custom sql query : SELECT training_courses.id, training_courses.training_id, training_courses.course_id,
I'm trying to create a complex SQL query (at least for me) but really
So I made a query like this global $connection; $query = SELECT * FROM
I have a ViewBag.People made up from a View; var query = db.Vw_INTERACTPEOPLE.Select(p =>
I have an array that's made up of data returned by a SQL query:
I have the following SQL query but it doesn't entirely work because it shows
I have query in SQL SERVER 2008 like this. And this is working. SELECT
Running this query in sql server 2008 now, but soon plan to move it
I recently built a query in SQL that I can use to look at

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.