我必须根据某些条件从一个超过1亿行的表中删除大约10K行.当我执行查询时,大约需要5分钟.我运行了一个解释计划(自MySQL不支持后delete转换为查询)并发现MySQL使用了错误的索引.select *explain delete
我的问题是:有没有办法告诉MySQL在删除期间使用哪个索引?如果没有,我该怎么办?选择临时表然后从临时表中删除?
我目前正在使用以下查询来获取一些数字:
SELECT gid, count(gid), (SELECT cou FROM size WHERE gid = infor.gid)
FROM infor
WHERE id==4325
GROUP BY gid;
Run Code Online (Sandbox Code Playgroud)
我目前阶段得到的输出如下:
+----------+-----------------+---------------------------------------------------------------+
| gid | count(gid) | (SELECT gid FROM size WHERE gid=infor.gid) |
+----------+-----------------+---------------------------------------------------------------+
| 19 | 1 | 19 |
| 27 | 4 | 27 |
| 556 | 1 | 556 |
+----------+-----------------+---------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
我试图计算加权平均值即
(1*19 + 4*27 + 1*556)/(19 + 27 + 556)
有没有办法使用单个查询执行此操作?
我记得在某个地方读过mongo引擎在文档的整个结构已经到位的情况下更加舒适,所以这是一个问题.
处理"空"数据时,例如插入空字符串时,我应该将其默认为null,""还是根本不插入?
{
_id: ObjectId("5192b6072fda974610000005"),
description: ""
}
Run Code Online (Sandbox Code Playgroud)
要么
{
_id: ObjectId("5192b6072fda974610000005"),
description: null
}
Run Code Online (Sandbox Code Playgroud)
要么
{
_id: ObjectId("5192b6072fda974610000005")
}
Run Code Online (Sandbox Code Playgroud)
您必须记住,description每个文档中可能填充或不填写该字段(基于用户输入).
考虑以下2个表:
Table A:
id
event_time
Table B
id
start_time
end_time
Run Code Online (Sandbox Code Playgroud)
表A中的每个记录都映射到表B中的恰好1个记录.这意味着表B没有重叠的句点.表A中的许多记录可以映射到表B中的相同记录.
我需要一个返回所有A.id,B.id对的查询.就像是:
SELECT A.id, B.id
FROM A, B
WHERE A.event_time BETWEEN B.start_time AND B.end_time
Run Code Online (Sandbox Code Playgroud)
我正在使用MySQL,我无法优化此查询.表A中有约980条记录,表B中有130.000条,这需要永远.我知道这必须执行980个查询,但是在一台强壮的机器上花费超过15分钟是很奇怪的.有什么建议?
PS我无法更改数据库架构,但我可以添加索引.但是,时间字段上的索引(带有1或2个字段)无济于事.
我要选择的列的内容text从entrytable.
EXPLAIN SELECT text
FROM entrytable
WHERE user = 'username' &&
`status` = '1' && (
`status_spam_user` = 'no_spam'
|| (
`status_spam_user` = 'neutral' &&
`status_spam_system` = 'neutral'
)
)
ORDER BY datum DESC
LIMIT 6430 , 10
Run Code Online (Sandbox Code Playgroud)
该表有三个指数:
EXPLAIN结果是:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE entrytable ref index_user,index_status_mit_spam index_user 32 const 7800 Using where; Using filesort
Run Code Online (Sandbox Code Playgroud)
possible_keys指数的MySQL可能要使用和keysMySQL的实际使用指标?index_status_mit_spam不使用索引?在查询中,列的顺序与索引中的顺序相同,...index_datum …我试图找出最好的方法,(在这种情况下可能无关紧要)找到一个表的行,基于标志的存在,以及另一个表中的行中的关系id.
这是模式:
CREATE TABLE files (
id INTEGER PRIMARY KEY,
dirty INTEGER NOT NULL);
CREATE TABLE resume_points (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
scan_file_id INTEGER NOT NULL );
Run Code Online (Sandbox Code Playgroud)
我正在使用SQLite3
文件表会非常大,通常为10K-5M行.resume_points将小于10K,只有1-2个不同scan_file_id的
所以我的第一个想法是:
select distinct files.* from resume_points inner join files
on resume_points.scan_file_id=files.id where files.dirty = 1;
Run Code Online (Sandbox Code Playgroud)
一位同事建议转弯:
select distinct files.* from files inner join resume_points
on files.id=resume_points.scan_file_id where files.dirty = 1;
Run Code Online (Sandbox Code Playgroud)
然后我想,因为我们知道不同的数量scan_file_id会很小,也许子选择是最优的(在这种罕见的情况下):
select * from files where id in (select distinct scan_file_id from resume_points); …Run Code Online (Sandbox Code Playgroud) 我有简单但很长的查询,它计算结果的内容大约需要14秒.主表上的计数本身不到一秒钟,但在多次加入后,延迟太高,如下所示
Select Count(Distinct visits.id) As Count_id
From visits
Left Join clients_locations ON visits.client_location_id = clients_locations.id
Left Join clients ON clients_locations.client_id = clients.id
Left Join locations ON clients_locations.location_id = locations.id
Left Join users ON visits.user_id = users.id
Left Join potentialities ON clients_locations.potentiality = potentialities.id
Left Join classes ON clients_locations.class = classes.id
Left Join professions ON clients.profession_id = professions.id
Inner Join specialties ON clients.specialty_id = specialties.id
Left Join districts ON locations.district_id = districts.id
Left Join provinces ON districts.province_id = provinces.id
Left Join …Run Code Online (Sandbox Code Playgroud) 我有一个带有一维数组的 JSON 字段。事实上,在这个字段中我有一些 ID 的列表,如下所示:
[347470, 162063, 17315, 346852, 174776, 295865, 7833, 136813]
Run Code Online (Sandbox Code Playgroud)
在我的查询中,我这样引用该字段:
... AND JSON_CONTAINS(`users_actions`, 174776)=0
Run Code Online (Sandbox Code Playgroud)
我的问题是:我应该为此字段创建一个索引吗?如果是的话,我应该使用哪个索引?
这个特定问题与将 mongodb 与 golang 包一起使用有关mongo-driver,但我认为这适用于大多数与 mongodb 的接口。
当Find用于从集合中查询某些数据时,我们可以同时使用bson.M- 和bson.D- 类型来指定此查找的过滤器。
bson.D如果元素的顺序很重要,bson.M则应根据文档使用,否则应使用。
D 是 BSON 文档的有序表示。当元素的顺序很重要时,应该使用这种类型,例如 MongoDB 命令文档。如果元素的顺序无关紧要,则应使用 M 代替。
现在我的问题是使用这些结构中的任何一个,即有序与无序,结构是否会对 mongo 查询优化器生成的查询计划产生影响。
在经典的 SQL 数据库中,顺序通常无关紧要,因为优化器足够聪明,可以使用汇总统计信息、索引等来确定首先执行哪些查询。
我是否可以假设这里也是这种情况,或者使用有序结构来查询我的集合是否会以某种方式干扰这一点/是否使用类似于使用优化器提示的有序结构工作?如果有一些干扰,这是否受到要搜索的字段是否被索引的影响?
我有一个用 .net / c# 开发的 Windows 服务。
\n它经常使用存储在 SQL Server 中的位置数据更新表。更新 SQL 查询非常慢,大约在 0.5-1.5 秒之间。该表中的记录不超过 1500 条。
\n你能帮我找到我想念的东西吗?这次更新应该有 sg,我有其他的可以完美快速地工作。
\n多谢!
\n桌子:
\nCREATE TABLE [LastPosition] (\n[lastposition_id] INTEGER IDENTITY NOT NULL,\n[unit_id] INTEGER NOT NULL,\n[date_time] DATETIME NOT NULL,\n[valid] TINYINT NOT NULL,\n[lat] REAL NOT NULL,\n[lon] REAL NOT NULL,\n[angle] SMALLINT NOT NULL,\n[speed] SMALLINT NOT NULL,\n[digit] TINYINT NOT NULL,\n[altitude] SMALLINT NULL,\n[sat] TINYINT NULL,\n[extrainfo] NVARCHAR(255) NOT NULL,\n[geoinfo] TINYINT NULL,\nCONSTRAINT LastPositionPrimaryKey PRIMARY KEY NONCLUSTERED(lastposition_id));\nRun Code Online (Sandbox Code Playgroud)\n询问:
\nexec sp_executesql N\'UPDATE [LastPosition] \n SET …Run Code Online (Sandbox Code Playgroud) mysql ×6
sql ×4
database ×3
mongodb ×2
explain ×1
go ×1
indexing ×1
json ×1
mongo-go ×1
nosql ×1
optimization ×1
sql-delete ×1
sql-server ×1
sqlite ×1
t-sql ×1