Skip to content
Advertisement

I need to optimize tables and queries

I have 3 tables: info, data, link, there is a request for data:

select *
from data,link,info
where link.info_id = info.id and link.data_id = data.id

offer optimization options: a) tables b) request. Queries for creating tables:

CREATE TABLE info (
        id int(11) NOT NULL auto_increment,
       name varchar(255) default NULL,
        desc text default NULL,
        PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

CREATE TABLE data (
        id int(11) NOT NULL auto_increment,
        date date default NULL,
        value INT(11) default NULL,
        PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

CREATE TABLE link (
        data_id int(11) NOT NULL,
        info_id int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

Thanks!

Advertisement

Answer

Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax:

select *
from data d join
     link l
     on l.data_id = d.id join
     info i
     on l.info_id = i.id;

Second, for this query your indexes are probably fine. I would also recommend a primary key index on link:

CREATE TABLE link (
    data_id int(11) NOT NULL,
    info_id int(11) NOT NULL,
    PRIMARY KEY (data_id, info_id)
);

This is a good idea in general, even if it is not specific to this query.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement