这可能是一个幼稚的问题,但这两个查询有什么区别,哪个是首选?
UPDATE table1,
(SELECT id,COUNT(*) idcount FROM table2 GROUP BY id) AS B
SET table1.Freq = B.idcount WHERE table1.id=B.id
Run Code Online (Sandbox Code Playgroud)
和
UPDATE table1 A INNER JOIN
(SELECT id,COUNT(*) idcount FROM table2 GROUP BY id) B USING (id)
SET A.Freq = B.idcount
Run Code Online (Sandbox Code Playgroud) 名为“messages”的表中有大约 5000 万行。问题是,即使使用索引,特定查询也运行得非常慢。我的背景更多是 Mysql,所以我更像是 Postgres (9.5) 的新手。任何解决此表查询缓慢问题的建议或帮助将不胜感激。
\n\n该表的结构如下:
\n\nmydatabase=# \\d+ messages\n\n\n id | integer | not null default nextval(\'messages_id_seq\'::regclass) | plain | | \n conversation_id | integer | | plain | | \n user_id | integer | | plain | | \n content | text | | extended | | \n attached_photos | text | | extended | | \n attached_video | text | | extended | | \n status | character varying(255) | | extended | | …Run Code Online (Sandbox Code Playgroud) 我网站的自动完成搜索功能搜索包含销售商品型号的 varchar 字段。
该字段可以包含 1 到 75 个字符的字符串,并且该表包含 400 000 行。我提出了一个仅从字符串开头搜索的查询,执行时间大约为 150-250 毫秒,这是可以接受的,但现在我的经理希望查询搜索任何子字符串,这会使查询速度慢 3-10 倍(大约 1000-2000 毫秒)。
我构建了一个 JS 小提琴,为您提供数据的示例以及两个查询的示例。
http://sqlfiddle.com/#!6/9efa3/2/0
表上已经有一些索引了。加速这个自动完成搜索字段的最佳实践是什么?(数据库版本为SQLSERVER 2008R2)
这是我正在处理的数据的一个简短示例:
CREATE TABLE [Products](
[productid] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[model] [nvarchar](75) NOT NULL
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[productid] ASC
));
insert into products values ('UMPX1AA0011 danish e-315 woot');
insert into products values ('P27y719VC');
insert into products values ('VG2y439m-LED');
insert into products values ('UMUyX165AAB01');
insert into products values ('U28y79VF');
insert into products values …Run Code Online (Sandbox Code Playgroud) performance sql-server sql-server-2008-r2 string-searching query-performance
我发现一个查询长时间卡在进程列表中,状态为“正在发送数据”。这个状态是什么意思?
选项1:
CREATE PROCEDURE [dbo].[GetStudents]
@MinimumAge int = NULL
AS
select * from Students s where @MinimumAge is null or s.Age >= @MinimumAge
Run Code Online (Sandbox Code Playgroud)
选项2:
CREATE PROCEDURE [dbo].[GetStudents]
@MinimumAge int = NULL
AS
IF @MinimumAge IS NULL
BEGIN
select * from Students s
END
ELSE
BEGIN
select * from Students s where s.Age >= @MinimumAge
END
Run Code Online (Sandbox Code Playgroud)
选项 1 会因为有额外的WHERE子句而比选项 2 慢吗?或者 SQL Server 会处理这个问题吗?
我认为选项 1 很好,因为我不必重复代码,除非它速度较慢。原来的程序有很多行代码。因此,我不想仅针对一种条件重复所有代码,除非这是唯一的好选择。
该列有索引age,并且不允许空值。
问题是,实际 SP 有很多行代码 - 所以我不想复制所有代码只是为了添加一个 where 条件,除非这是唯一好的选择。
performance null sql-server stored-procedures query-performance
我想根据 ID 获取最多 100 行。id 是表的主键。
我编写的查询如下所示:
select * from table where id = any ($1);
Run Code Online (Sandbox Code Playgroud)
其中$1被插值为 ids 数组。
使用时EXPLAIN ANALYZE我得到以下计划(解释链接):
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.43..44.98 rows=17 width=553) (actual time=100.048..834.209 rows=17 loops=1)
-> Index Scan using instagram_id_index_1000 on profiles_1000 (cost=0.43..44.98 rows=17 width=553) (actual time=100.046..834.163 rows=17 loops=1)
Index Cond: (id = ANY ('{34491540,28977916,33241270,33609141,31043380,29364420,30247037,33311491,36267571,32886281,32366574,32569254,33038689,31089076,29416100,30455309,31570597}'::integer[]))
Planning time: 424.512 ms
Execution time: 834.280 ms
(5 rows)
Run Code Online (Sandbox Code Playgroud)
当我实际执行它时(使用\timing),我得到的结果在 2-5 秒范围内!我真的无法接受如此糟糕的表现。EXPLAIN ANALYZE首先提供的执行时间就已经很长了。
一些上下文:
1)数据库是本地的,所以没有网络延迟
2)我查询的表是物化视图
3)我也尝试了 …
我继承了一个数据库,其中许多表都将 bigint 作为许多字段的数据类型,当您看到内容时,它并不需要 bigint 提供的所有空间。对于不需要的字段使用bigint会影响数据库性能吗?
以下查询在约 60 个数据库上并行运行。在没有提示的情况下,至少 10% 的数据库存在大量溢出和非最佳计划。
使用更大的数据库作为指导,查询被锁定并带有提示(1 个 CPU 上约 75 毫秒),以减少运行时的差异,因为 1 个错误的计划会导致整体运行时间终止。我们主要反对让每个DB自由调整其计划,因为从长远来看,某些DB可能会在生产平台上着火。我们对大型数据库的近乎最佳计划感到非常满意,但对于较小的数据库可能不是最佳的。
即使在添加带有完整扫描的统计信息后,一些(~5)较小的数据库仍然表现出小的 1 级溢出(参见计划)。运行时间仍然可以(125 毫秒),但希望消除溢出。
这是 Sql Server 2019。自适应授权功能 (2017) 是否应该因溢出而调整授权?在 SSMS 和查看计划中重复运行它似乎表明没有变化。
select top (@pMax)
aig.ObjectId,
iif((@pA in (1, 2, 3, 4, 5, 6, 9, 11, 12) and ttm.ObjectId is not null) or
(@pA in (7, 8, 10, 13, 14, 15)), 1.0, 0.0) as Rank
from oav.value aig
inner merge join Pub.CachedObjectHierarchyAttributes coha
on coha.ObjectId = aig.ObjectId
and coha.IsActiveForPublisher = 1
and coha.IsToolItem = 1
inner merge …Run Code Online (Sandbox Code Playgroud) sql-server execution-plan cardinality-estimates query-performance
为了优化 SELECT 语句,我尝试在 MySQL 中使用以下 SQL 语句使索引唯一:
ALTER TABLE credentials DROP INDEX special_credential_id, ADD UNIQUE KEY special_credential_id(special_credential_id)
我的问题是:这是一笔交易吗?这意味着如果创建唯一索引失败,旧的special_credential_id索引是否仍然存在?通常创建一个新索引很容易,但我们讨论的是包含 100 个 Mio 条目的表。
我有一个使用SUBSTRINGWHERE 子句中的函数的存储过程。
SELECT DISTINCT
ColumnBB,
ColumnCC
FROM TableAA WITH (NOLOCK)
WHERE SUBSTRING(ColumnAA, 1, 17) = @VariableA;
Run Code Online (Sandbox Code Playgroud)
如何防止此查询的索引扫描并使其快速执行?
performance ×8
sql-server ×5
mysql ×3
postgresql ×2
index ×1
join ×1
mariadb ×1
null ×1
optimization ×1
slow-log ×1
subquery ×1
transaction ×1
update ×1