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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:07:32+00:00 2026-06-02T18:07:32+00:00

I have a table to store tag name for posts table: tagname tags |

  • 0

I have a table to store tag name for posts

table: tagname
tags                    |  pid
festival                | 10034
New York Fashion Week   | 10034
festival                | 10035
car                     | 10036
...

The table now has already 590,000 records. Now I want to get the first 10 most popular tags from this table.

SELECT tags, COUNT(*) as Num FROM tagname
GROUP BY tags
ORDER BY Num DESC

This will cost 23.88 seconds. return 358 kinds of tags

tags       |  Num
festival   |  7201
art        |  6988
gift       |  6755
...

How to optimization this query, even in my.cnf? I tried to add index for tags, it seems with no effect.

EDIT:
EXPLAIN SELECT tags, COUNT(tags) as Num FROMtagnameGROUP BY tags order by Num DESC

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE      tagname ALL     NULL    NULL     NULL    NULL   597649  Using temporary; Using filesort
  • 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-02T18:07:34+00:00Added an answer on June 2, 2026 at 6:07 pm

    Add an index to your tags column:

    ALTER TABLE `tagname` 
    ADD INDEX `tags_index` (`tags` ASC) ;
    

    EDIT:

    Try creating a second index

    CREATE INDEX tags_pid_index ON tagname (tags, pid);
    

    Then modify your query to:

    SELECT tags, COUNT(pid) as Num 
    FROM tagname
    GROUP BY tags
    ORDER BY Num DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that store Tags like so: ProductTags TagID PK Name nvarchar(50)
I have column in table where i store tag ids as 1|5|10 I want
I have stored my tags in the SQL Server database, TABLE NAME: Tags COLUMNS
I have these three table to store tags for photo albums. Here is the
I have a Tags table whose schema consists of only ID and Name (unique).
I love NHibernate's ability to have one table to store multiple types based on
We have a table in which we store millions of small BLOBs (2-10kb). And
I have a table in which users store scores and other information about said
I have a table with sales per store as follows: SQL> select * from
I have a table with countrycodes, whats the best way to store these 2

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.