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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:58:57+00:00 2026-05-16T14:58:57+00:00

im running bellow query to fetch from 3 mysql tables and it works fine

  • 0

im running bellow query to fetch from 3 mysql tables and it works fine but it increases memory usage and sometime it takes 10 seconds to start loading the page

  INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))
  INNER JOIN table_topics AS nto ON (ns.associated = '' OR CONCAT(' ',COALESCE(ns.associated,'-'),' ') LIKE CONCAT('%',nto.topicid,'%' ))

problem is in Inner Join statemnet and if i remove

ns.tags = ” OR

and

ns.associated = ” OR

the memory overuse problem will be fixed but cant show stories with empty tag field

is there any other way to write bellow statement to include all stories even those with no tags !?

  INNER JOIN table_tags AS ntg ON (ns.tags = '' OR CONCAT(' ',COALESCE(ns.tags,' '),' ') LIKE CONCAT('% ',ntg.tid,' %'))

my tags id are stored in table_ns like ( 14 17 2 ) seprated with space

  • 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-16T14:58:58+00:00Added an answer on May 16, 2026 at 2:58 pm

    Your condition ns.tags = '' OR ... means that if ns.tags = '' is true this record from table_stories will be joined with all the records in table_tags. Same with ns.associated = '' OR ... This can easily “create” huuuge result sets and that’s most likely not what you want.
    If you really don’t want/can’t change/normalize the table structure (as you’ve stated in a comment to my previous answer) my best guess is to use two LEFT JOINs like:

    SELECT
      ns.*,
      ntg.tid,
      nto.topicid,
      group_concat(DISTINCT ntg.tag ) as mytags,
      group_concat(DISTINCT ntg.slug ) as tagslug,
      group_concat(DISTINCT nto.topicname ) as mytopics,
      group_concat(DISTINCT nto.slug ) as topicslug,
      ns.counter AS counter_num
    FROM
      table_stories AS ns
    LEFT JOIN
      table_tags AS ntg
    ON
      CONCAT(' ', ns.tags,' ') LIKE CONCAT('% ',ntg.tid,' %')
    LEFT JOIN
      table_topics AS nto
    ON
      CONCAT(' ', ns.associated, ' ') LIKE CONCAT('%',nto.topicid,'%' )
    WHERE
      time <= '4711'
    GROUP BY
      ns.sid
    ORDER BY
      ns.sid DESC,ns.hotnews DESC
    

    But MySQL can’t use indices for this, so the query will result in full table scans. It will also need temporary tables and filesort, i.e. the query is relatively slow.
    Without changing the table structure but the format of the data from 1 2 3 to 1,2,3 you can at least get rid of Concat() and LIKE by using the slightly faster FIND_IN_SET(str, strlist).


    Just for completeness and in case you change your mind: http://en.wikipedia.org/wiki/Database_normalization

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

Sidebar

Related Questions

I am running the query below in Sybase application which works great but the
I can find tons of examples where you can query a MySQL database from
Upon running the below SQL statement (which works perfectly) I've found that all tables
I am running the below query to extract data from an xml column...question is
I have these two tables setup and the below query running. The problem is
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
I am running the following MySQL query to filter the number of listings with
I'm running ant and added the below to build.xml but I'm getting:\ build.xml:51: Element
I have a MySql database with just over 30 Million rows, but some of
Is it possible to get around mysql_unbuffered_query()'s limitation of having one query running 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.