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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:48:44+00:00 2026-05-17T02:48:44+00:00

I need some performance improvement guidance, my query takes several seconds to run and

  • 0

I need some performance improvement guidance, my query takes several seconds to run and this is causing problems on the server. This query runs on the most common page on my site. I think a radical rethink may be required.

~ EDIT ~
This query produces a list of records whose keywords match those of the program (record) being queried. My site is a software download directory. And this list is used on the program listing page to show other similar programs. PadID is the primary key of the program records in my database.

~ EDIT ~

Heres my query

 select match_keywords.PadID, count(match_keywords.Word) as matching_words 
 from keywords current_program_keywords 
 inner join keywords match_keywords on
       match_keywords.Word=current_program_keywords.Word 
 where match_keywords.Word IS NOT NULL 
 and current_program_keywords.PadID=44243 
 group by match_keywords.PadID 
 order by matching_words DESC 
 LIMIT 0,11;

Heres the query explained.
alt text

Heres some sample data, however I doubt you’d be able to see the effects of any performance tweaks without more data, which I can provide if you’d like.

 CREATE TABLE IF NOT EXISTS `keywords` (
   `Word` varchar(20) NOT NULL,
   `PadID` bigint(20) NOT NULL,
   `LetterIdx` varchar(1) NOT NULL,
   KEY `Word` (`Word`),
   KEY `LetterIdx` (`LetterIdx`),
   KEY `PadID_2` (`PadID`,`Word`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 INSERT INTO `keywords` (`Word`, `PadID`, `LetterIdx`) VALUES
 ('tv', 44243, 'T'),
 ('satellite tv', 44243, 'S'),
 ('satellite tv to pc', 44243, 'S'),
 ('satellite', 44243, 'S'),
 ('your', 44243, 'X'),
 ('computer', 44243, 'C'),
 ('pc', 44243, 'P'),
 ('soccer on your pc', 44243, 'S'),
 ('sports on your pc', 44243, 'S'),
 ('television', 44243, 'T');

I’ve tried adding an index, but this doesn’t make much difference.

 ALTER TABLE `keywords` ADD INDEX ( `PadID` ) 
  • 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-17T02:48:45+00:00Added an answer on May 17, 2026 at 2:48 am

    You might find this helpful if I understood you correctly. The solution takes advantage of innodb’s clustered primary key indexes (http://pastie.org/1195127)

    EDIT: here’s some links that may prove of interest:

    http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html

    http://dev.mysql.com/doc/refman/5.0/en/innodb-adaptive-hash.html

    drop table if exists programmes;
    create table programmes
    (
    prog_id mediumint unsigned not null auto_increment primary key,
    name varchar(255) unique not null
    )
    engine=innodb;
    
    insert into programmes (name) values 
    ('prog1'),('prog2'),('prog3'),('prog4'),('prog5'),('prog6');
    
    
    drop table if exists keywords;
    create table keywords
    (
    keyword_id mediumint unsigned not null auto_increment primary key,
    name varchar(255) unique not null
    )
    engine=innodb;
    
    insert into keywords (name) values 
    ('tv'),('satellite tv'),('satellite tv to pc'),('pc'),('computer');
    
    
    drop table if exists programme_keywords;
    create table programme_keywords
    (
    keyword_id mediumint unsigned not null,
    prog_id mediumint unsigned not null,
    primary key (keyword_id, prog_id), -- note clustered composite primary key
    key (prog_id)
    )
    engine=innodb;
    
    insert into programme_keywords values 
    
    -- keyword 1
    (1,1),(1,5),
    
    -- keyword 2
    (2,2),(2,4),
    
    -- keyword 3
    (3,1),(3,2),(3,5),(3,6),
    
    -- keyword 4
    (4,2),
    
    -- keyword 5
    (5,2),(5,3),(5,4);
    
    /*
    efficiently list all other programmes whose keywords match that of the 
    programme currently being queried (for instance prog_id = 1) 
    */
    
    
    drop procedure if exists list_matching_programmes;
    
    delimiter #
    
    create procedure list_matching_programmes
    (
    in p_prog_id mediumint unsigned
    )
    proc_main:begin
    
    select
     p.*
    from
     programmes p
    inner join
    (
     select distinct -- other programmes with same keywords as current
      pk.prog_id
     from
      programme_keywords pk
     inner join
     (
      select keyword_id from programme_keywords where prog_id = p_prog_id
     ) current_programme -- the current program keywords
     on pk.keyword_id = current_programme.keyword_id
     inner join programmes p on pk.prog_id = p.prog_id 
    
    ) matches 
    on matches.prog_id = p.prog_id
    order by
     p.prog_id;
    
    end proc_main #
    
    
    delimiter ;
    
    call list_matching_programmes(1);
    call list_matching_programmes(6); 
    
    
    explain
    select
     p.*
    from
     programmes p
    inner join
    (
     select distinct
      pk.prog_id
     from
      programme_keywords pk
     inner join
     (
      select keyword_id from programme_keywords where prog_id = 1
     ) current_programme
     on pk.keyword_id = current_programme.keyword_id
     inner join programmes p on pk.prog_id = p.prog_id 
    
    ) matches 
    on matches.prog_id = p.prog_id
    order by
     p.prog_id;
    

    EDIT: added char_idx functionality as requested

    alter table keywords add column char_idx char(1) null after name;
    
    update keywords set char_idx = upper(substring(name,1,1));
    
    select * from keywords;
    
    explain
    select
     p.*
    from
     programmes p
    inner join
    (
     select distinct
      pk.prog_id
     from
      programme_keywords pk
     inner join
     (
      select keyword_id from keywords where char_idx = 'P' -- just change the driver query
     ) keywords_starting_with
     on pk.keyword_id = keywords_starting_with.keyword_id
    ) matches 
    on matches.prog_id = p.prog_id
    order by
     p.prog_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across this question several times, you need to do some performance
I need some clarification on the performance of a website. Assume server configuration is
Need some help to solve this. I have a gridview and inside the gridview
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
Need some help with a query.. I have three tables. Source id name 1
I have a project with Maven build and need to add some basic performance
I am doing some performance tests using .Net 3.5 against SQL Server. I am
The Problem I need some properties of each item that query returns as array

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.