我需要在表中插入大量数据(每秒约50个查询),这是表结构所使用的。
CREATE TABLE `word` (
`title` varchar(55) NOT NULL,
`company` tinyint(4) NOT NULL,
`known` tinyint(4) NOT NULL,
`appearances` smallint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `word`
ADD PRIMARY KEY (`title`,`company`),
ADD UNIQUE KEY `word` (`company`,`title`),
这是一些示例查询
INSERT INTO `word` (`title`, `company`, `known`) VALUES('retrogaming', 2, 0) ON DUPLICATE KEY UPDATE `appearances` = `appearances` + 1
INSERT INTO `word` (`title`, `company`, `known`) VALUES('gamedev', 2, 0) ON DUPLICATE KEY UPDATE `appearances` = `appearances` + 1
INSERT INTO `word` (`title`, `company`, `known`) VALUES('retrogamer', 2, 0) ON DUPLICATE KEY UPDATE `appearances` = `appearances` + 1
我认为查询执行速度会变慢,因为5秒钟后我执行了332条查询,但仅成功返回了125条。
当我手动执行这些查询之一时,大约需要0.01760秒,考虑到我需要执行多少时间,这相当慢。我知道该表由于使用双键会比较慢,但是我没想到它会这么大。 title
和company
应该一起形成唯一的密钥。
编辑:
这是我在节点中用于插入的代码
const con1 = mysql.createConnection(require('../mysql1.json'));
con1.connect();
function analyzeWords(tweet, array, company) {
let splitTweet = tweet.split(` `);
for (let i = 0; i < splitTweet.length; i ++) {
let word = splitTweet[i];
let index = inArray(word, knownWords);
if (index) {
submitWord(word, 1, company);
} else {
submitWord(word, 0, company);
}
}
}
function submitWord(word, known, company) {
if (word.length <= 55) {
let sql = `INSERT INTO \`word\` (\`title\`, \`company\`, \`known\`) VALUES('${word}', ${company}, ${known}) ON DUPLICATE KEY UPDATE \`appearances\` = \`appearances\` + 1`;
con1.query(sql, (error, results) => { if(error) console.log(error) });
}
}
每个句子都传递到analyzeWords()
中,因此我可以跟踪每个“公司”的单词用法
答案 0 :(得分:0)
好吧,我将我的MySQL服务器移到了基于数字海洋ssd的小滴上,它现在能够完美保持运行状态,查询需要0.0012秒