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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:40:00+00:00 2026-05-28T00:40:00+00:00

I am trying to improve my SQL query so that i could get only

  • 0

I am trying to improve my SQL query so that i could get only the newest information from f_facts table and i’m checking if the information is new by the date_id

for some reason this query isn’t working for me:

REPLACE INTO `r_views`
        SELECT network_id, type_id, topic_id, COUNT(*), date_id, 
        SUM( IF (`d_refer`.`refer` LIKE '%facebook%', 1,0) ) AS `facebook`, 
        SUM( IF (`d_refer`.`refer` LIKE '%twitter%', 1,0) ) AS `twitter`, 
        SUM( IF ( (`d_refer`.`refer` LIKE '%hotmail%' or `d_refer`.`refer` LIKE '%live%' or `d_refer`.`refer` LIKE '%gmail%' or `d_refer`.`refer` LIKE '%mail.%'), 1,0) ) AS `email`, 
        SUM( IF (`d_refer`.`refer` LIKE '%google%', 1,0) ) AS `google`      

        FROM `f_facts` 
        LEFT JOIN `d_date` ON `f_facts`.`date_id` = `d_date`.`id` 
        LEFT JOIN `d_refer` ON `f_facts`.`refer_id` = `d_refer`.`id` 

        WHERE `action_id`=3 AND `type_id` != 17 AND `date_id` > (SELECT `date_id` FROM `r_views` ORDER BY `date_id` DESC LIMIT 1) GROUP BY topic_id, date_id

This is my older sql query that was reading all of the data from f_facts table which is not efficient:

REPLACE INTO `r_views`
        SELECT network_id, type_id, topic_id, COUNT(*), date_id, 
        SUM( IF (`d_refer`.`refer` LIKE '%facebook%', 1,0) ) AS `facebook`, 
        SUM( IF (`d_refer`.`refer` LIKE '%twitter%', 1,0) ) AS `twitter`, 
        SUM( IF ( (`d_refer`.`refer` LIKE '%hotmail%' or `d_refer`.`refer` LIKE '%live%' or `d_refer`.`refer` LIKE '%gmail%' or `d_refer`.`refer` LIKE '%mail.%'), 1,0) ) AS `email`, 
        SUM( IF (`d_refer`.`refer` LIKE '%google%', 1,0) ) AS `google`      

        FROM `f_facts` 
        LEFT JOIN `d_date` ON `f_facts`.`date_id` = `d_date`.`id` 
        LEFT JOIN `d_refer` ON `f_facts`.`refer_id` = `d_refer`.`id` 

        WHERE `action_id`=3 AND `type_id` != 17 GROUP BY topic_id, date_id 
  • 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-28T00:40:01+00:00Added an answer on May 28, 2026 at 12:40 am

    I think you can enhance the query in three points:

    1) the email column you can use this instead of the ORs:

    SUM( IF ( (`d_refer`.`refer` LIKE '%live%' or `d_refer`.`refer` LIKE '%mail.%'), 1,0) ) AS `email`
    

    2) Do you really need the two Joins? They seem to me a little bit redundant.

    3) Add an index on the date

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

Sidebar

Related Questions

I'm trying to populate a table with user information in a MS SQL database
We have a SQL table that is populated with events from our website (mostly
I am trying to improve query readability by using a function in SQL Server
I'm trying to improve performance in our app. I've got performance information in the
Hi all I have a sql query SELECT FKid1, FKID2, a, b, c from
I have an sql scenario as follows which I have been trying to improve.
Ok.. So I'm trying to improve my SQL skills and have a question. Here
I have a query that strips data from a XML string, inserts it into
To improve my skills on Hector and cassandra I'm trying diffrent methods to query
I'm having trouble trying to optimize the following query for sql server 2005. Does

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.