sql-execution-plan

PostgreSQL performance of ad-hoc SQL vs functions

给你一囗甜甜゛ 提交于 2019-12-24 06:29:28
问题 Is there any difference? I know SQL queries are having their execution plans cached just as good as functions. I foud someone telling: Performance is an issue, and we suspect query planning might be an underlying cause. I've rewritten the scripts from ad-hoc SQL to a Postgres functions (CREATE FUNCTION) and we saw server load go down quite a bit. But why? 回答1: The query plan for ad-hoc queries is not cached, only for prepared statements. And PL/pgSQL functions handle all SQL statements like

Adding inner query is not changing the execution plan

扶醉桌前 提交于 2019-12-24 01:14:31
问题 Consider the following queries. select * from contact where firstname like '%some%' select * from (select * from contact) as t1 where firstname like '%some%' The execution plans for both queries are same and executes at same time. But, I was expecting the second query will have a different plan and execute more slowly as it has to select all data from contact and apply filter. It looks like I was wrong. I am wondering how this is happening? Database Server : SQL server 2005 回答1: The "query

Postgres uses wrong index in query plan

痴心易碎 提交于 2019-12-24 00:59:02
问题 Below I have 2 almost identical queries, only the limit is different. Nevertheless, the query plan and execution time are totally different. The first query is +300 times slower than the second one. The problem only occurs for a small number of owner_ids. Owners with many routes (+1000), none of which has recently been edited. The table route contains 2,806,976 rows. The owner in the example has 4,510 routes. The database is hosted on Amazon RDS on a server with 34.2 GiB memory, 4vCPU and

Sql indexes vs full table scan

点点圈 提交于 2019-12-24 00:52:50
问题 While writing complex SQL queries, how do we ensure that we are using proper indexes and avoiding full table scans? I do it by making sure I only join on columns that have indexes(primary key, unique key etc). Is this enough? 回答1: Look at the execution plan of the query to see how the query optimizer thinks things must be retrieved. The plan is generally based on the statistics on the tables, the selectivity of the indices and the order of the joins. Note that the optimizer can decide that

Date Parameter causing Clustered Index Scan

*爱你&永不变心* 提交于 2019-12-23 22:56:59
问题 I have the following query DECLARE @StartDate DATE = '2017-09-22' DECLARE @EndDate DATE = '2017-09-23' SELECT a.col1, a.col2, b.col1, b.col2, b.col3, a.col3 FROM TableA a JOIN TableB b ON b.pred = a.pred WHERE b.col2 > @StartDate AND b.col2 < @EndDate When I run this and inspect the actual execution plan, I can see that the most costly operator is a clustered index scan (The index is on a.pred) However, if I change the query as follows SELECT a.col1, a.col2, b.col1, b.col2, b.col3, a.col3

Postgres query is very slow when using a parameter instead of an hardcoded string

耗尽温柔 提交于 2019-12-23 12:18:25
问题 I am running into this Postgres issue where the same query takes a long time to execute if I use a parameter vs hardcoding its value on the query string. The column name is 'media_type' and it's a VARCHAR(20). I am running these queries from PHP, using Symfony2 and Doctrine2 ORM and the table in question has about 1.000.000 records. Do I have a problem on my query? Can it be a Postgres configuration problem? 1 - Hardcoded value for media_type duration: 5.365 ms parse pdo_stmt_00000001: SELECT

Oracle execution plan cost vs speed

白昼怎懂夜的黑 提交于 2019-12-23 05:03:54
问题 When building and tuning a query in Oracle, speed is generally the main concern for the developer. However, in tuning a particular query, I recently tried the FIRST_ROWS and NO_CPU_COSTING hints and an execution plan was generated that is 80% faster than the previous plan in execution time, but at a 300% higher cost. There is very little I/O in the execution plan, and it appears that all the additional cost comes from a nested loop outer join between two views. This query is paginated, so I

What is an automatic covering index?

醉酒当歌 提交于 2019-12-22 04:26:12
问题 When using EXPLAIN QUERY PLAN in SQLite 3 it sometimes gives me output such as SEARCH TABLE staff AS s USING AUTOMATIC COVERING INDEX (is_freelancer=? AND sap=?) (~6 rows) Where does the index come from and what does it do? The table has no manually created indices on it. 回答1: "Automatic" means that SQLite creates a temporary index that is used only for this query, and deleted afterwards. This happens when the cost of creating the index is estimated to be smaller than the cost of looking up

Data mismatch when querying with different indexes

血红的双手。 提交于 2019-12-21 18:35:27
问题 I stumbled upon with a very curious case. We have a SQL Server 2012 database and such a table CREATE TABLE [dbo].[ActiveTransactions] ( [Id] [BIGINT] IDENTITY(1,1) NOT NULL, [Amount] [DECIMAL](12, 4) NOT NULL, [TypeId] [SMALLINT] NOT NULL, [GameProviderId] [SMALLINT] NULL, [UserId] [INT] NOT NULL, [Checksum] [NVARCHAR](150) NOT NULL, [Date] [DATETIME2](7) NOT NULL, [ExternalKey] [VARCHAR](60) NULL, [ExternalDescription] [NVARCHAR](1000) NULL, [OperatorId] [SMALLINT] NULL, [GameId] [NVARCHAR]

Data mismatch when querying with different indexes

时间秒杀一切 提交于 2019-12-21 18:35:27
问题 I stumbled upon with a very curious case. We have a SQL Server 2012 database and such a table CREATE TABLE [dbo].[ActiveTransactions] ( [Id] [BIGINT] IDENTITY(1,1) NOT NULL, [Amount] [DECIMAL](12, 4) NOT NULL, [TypeId] [SMALLINT] NOT NULL, [GameProviderId] [SMALLINT] NULL, [UserId] [INT] NOT NULL, [Checksum] [NVARCHAR](150) NOT NULL, [Date] [DATETIME2](7) NOT NULL, [ExternalKey] [VARCHAR](60) NULL, [ExternalDescription] [NVARCHAR](1000) NULL, [OperatorId] [SMALLINT] NULL, [GameId] [NVARCHAR]