简体   繁体   English

优化MySQL表结构。 需要建议

[英]Optimizing MySQL table structure. Advice needed

I have these table structures and while it works, using EXPLAIN on certain SQL queries gives 'Using temporary; 我具有这些表结构,并且在可以工作时,在某些SQL查询上使用EXPLAIN会给出“使用临时; Using filesort' on one of the table. 在表之一上使用“文件排序”。 This might hamper performance once the table is populated with thousands of data. 一旦在表中填充了数千个数据,这可能会影响性能。 Below are the table structure and explanations of the system. 下表是系统的表格结构和说明。

CREATE TABLE IF NOT EXISTS `jobapp` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fullname` varchar(50) NOT NULL,
  `icno` varchar(14) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `timestamp` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `icno` (`icno`)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `jobapplied` (
  `appid` int(11) NOT NULL,
  `jid` int(11) NOT NULL,
  `jobstatus` tinyint(1) NOT NULL,
  `timestamp` int(10) NOT NULL,
  KEY `jid` (`jid`),
  KEY `appid` (`appid`)
) ENGINE=MyISAM;

Query I tried which gives aforementioned statement: 我试过的查询给出了上述声明:

EXPLAIN SELECT japp.id, japp.fullname, japp.icno, japp.status, japped.jid, japped.jobstatus
FROM jobapp AS japp
INNER JOIN jobapplied AS japped ON japp.id = japped.appid
WHERE japped.jid = '85'
AND japped.jobstatus = '2'
AND japp.status = '2'
ORDER BY japp.`timestamp` DESC 

This system is for recruiting new staff. 该系统用于招聘新员工。 Once registration is opened, hundreds of applicant will register in a single time. 一旦注册开始,数百名申请人将一次注册。 They are allowed to select 5 different jobs. 他们被允许选择5个不同的工作。 Later on at the end of registration session, the admin will go through each job one by one. 稍后在注册会话结束时,管理员将逐项处理每个作业。 I have used a single table (jobapplied) to store 2 items (applicant id, job id) to record who applied what. 我使用了一个表(jobapplied)来存储2个项目(申请人id,工作id)来记录谁申请了什么。 And this is the table which causes aforementioned statement. 这就是导致上述陈述的表格。 I realize this table is without PRIMARY key but I just can't figure out any other way later on for the admin to search specifically which job who have applied. 我意识到此表没有PRIMARY键,但后来我再也找不到其他方法供管理员专门搜索已申请的职位。

Any advice on how can I optimize the table? 关于如何优化表格的任何建议?

Apart from the missing indexes and primary keys others have mentioned . 除了缺少索引和主键外,还提到了其他内容。 . .

This might hamper performance once the table is populated with thousands of data. 一旦在表中填充了数千个数据,这可能会影响性能。

You seem to be assuming that the query optimizer will use the same execution plan on a table with thousands of rows as it will on a table with just a few rows. 您似乎假设查询优化器将在具有数千行的表上使用与仅具有几行的表相同的执行计划。 Optimizers don't work like that. 优化器不是那样工作的。

The only reliable way to tell how a particular vendor's optimizer will execute a query on a table with thousands of rows--which is still a small table, and will probably easily fit in memory--is to 告诉特定供应商的优化程序如何对具有数千行的表执行查询的唯一可靠方法-这仍然是一个小表,并且很可能容易放入内存中-

  • load a scratch version of the database with thousands of rows 加载具有数千行的数据库的临时版本
  • "explain" the query you're interested in “解释”您感兴趣的查询

FWIW, the last test I ran like this involved close to a billion rows--about 50 million in each of about 20 tables. FWIW,我最后一次像这样进行的测试涉及将近10亿行-大约20个表中的每个表大约有5000万行。 The execution plan for that query--which included about 20 left outer joins--was a lot different than it was for the sample data (just a few thousand rows). 该查询的执行计划(包括大约20个左外部联接)与示例数据(仅几千行)有很大不同。

您正在按jobapp.timestamp进行排序,但是没有时间戳的索引,因此表排序(可能是临时的)将是必要的,请尝试为jobapp添加时间戳并为其添加索引,例如KEY timid(timestamp,id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM