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

The Archive Base Latest Questions

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

I am writing a script which will list 25 items of all 12 categories.

  • 0

I am writing a script which will list 25 items of all 12 categories. Database structure is like:

tbl_items
---------------------------------------------
item_id | item_name | item_value | timestamp 
---------------------------------------------

tbl_categories
-----------------------------
cat_id | item_id | timestamp
-----------------------------

There are around 600,000 rows in the table tbl_items. I am using this SQL query:

SELECT e.item_id, e.item_value
  FROM tbl_items AS e
  JOIN tbl_categories AS cat WHERE e.item_id = cat.item_id AND cat.cat_id = 6001
  LIMIT 25

Using the same query in a loop for cat_id from 6000 to 6012. But I want the latest records of every category. If I use something like:

SELECT e.item_id, e.item_value
  FROM tbl_items AS e
  JOIN tbl_categories AS cat WHERE e.item_id = cat.item_id AND cat.cat_id = 6001
  ORDER BY e.timestamp
  LIMIT 25

..the query goes computing for approximately 10 minutes which is not acceptable. Can I use LIMIT more nicely to give the latest 25 records for each category?

Can anyone help me achieve this without ORDER BY? Any ideas or help will be highly appreciated.

EDIT

tbl_items

+---------------------+--------------+------+-----+---------+-------+
| Field               | Type         | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+-------+
| item_id             | int(11)      | NO   | PRI | 0       |       |
| item_name           | longtext     | YES  |     | NULL    |       |
| item_value          | longtext     | YES  |     | NULL    |       |
| timestamp           | datetime     | YES  |     | NULL    |       |
+---------------------+--------------+------+-----+---------+-------+

tbl_categories

+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| cat_id         | int(11)    | NO   | PRI | 0       |       |
| item_id        | int(11)    | NO   | PRI | 0       |       |
| timestamp      | datetime   | YES  |     | NULL    |       |
+----------------+------------+------+-----+---------+-------+
  • 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-28T13:49:10+00:00Added an answer on May 28, 2026 at 1:49 pm

    First of all:

    It seems to be a N:M relation between items and categories: a item may be in several categories. I say this because categories has item_id foreign key.

    If is not a N:M relationship then you should consider to change design. If it is a 1:N relationship, where a category has several items, then item must constain category_id foreign key.

    Working with N:M:

    I have rewrite your query to make a inner join insteat a cross join:

      SELECT e.item_id, e.item_value
      FROM 
         tbl_items AS e
      JOIN 
         tbl_categories AS cat 
            on e.item_id = cat.item_id
      WHERE  
         cat.cat_id = 6001
      ORDER BY 
         e.timestamp
      LIMIT 25
    

    To optimize performance required indexes are:

    create index idx_1 on tbl_categories( cat_id, item_id)
    

    it is not mandatory an index on items because primary key is also indexed.
    A index that contains timestamp don’t help as mutch. To be sure can try with an index on item with item_id and timestamp to avoid access to table and take values from index:

    create index idx_2 on tbl_items( item_id, timestamp)
    

    To increase performace you can change your loop over categories by a single query:

      select T.cat_id, T.item_id, T.item_value from 
      (SELECT cat.cat_id, e.item_id, e.item_value
       FROM 
         tbl_items AS e
       JOIN 
         tbl_categories AS cat 
            on e.item_id = cat.item_id
       ORDER BY 
         e.timestamp
       LIMIT 25
      ) T
      WHERE  
         T.cat_id between 6001 and 6012
      ORDER BY
         T.cat_id, T.item_id
    

    Please, try this querys and come back with your comments to refine it if necessary.

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

Sidebar

Related Questions

I'm writing a script which will have to work on directories which are modified
I am writing a R script which I will be running as a script
I'm writing a shell script which will rsync files from remote machines, some linux,
I am writing a shell script which will read a user input and do
I am writing a Tcl script which will be used on an embedded device.
Hi I am writing a small jQuery script which will toggle a hidden Top-Panel
I am writing a script which will display a stock chart as ASCII art
I'm writing a php script which will enable people to change the theme of
I'm writing a small Python script which will periodically pull information from a 3rd
I am writing a script which will add a new project in the repository,

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.