我使用SQLite数据库并在执行实际查询之前运行EXPLAIN语句,以验证是否有任何尝试在数据库上写入.
现在,我们已迁移到SQL Server,我需要知道查询是否尝试在数据库上写入,或者只是一个简单的SELECT语句.我基本上试图避免任何恶意声明.
我有两个简单的表:
CREATE TABLE cat_urls (
Id int(11) NOT NULL AUTO_INCREMENT,
SIL_Id int(11) NOT NULL,
SiteId int(11) NOT NULL,
AsCatId int(11) DEFAULT NULL,
Href varchar(2048) NOT NULL,
ReferrerHref varchar(2048) NOT NULL DEFAULT '',
AddedOn datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
GroupId int(11) DEFAULT NULL,
PRIMARY KEY (Id),
INDEX SIL (SIL_Id, AsCatId)
)
CREATE TABLE products (
Id int(11) NOT NULL AUTO_INCREMENT,
CatUrlId int(11) NOT NULL,
Href varchar(2048) NOT NULL,
SiteIdentity varchar(2048) NOT NULL,
Price decimal(12, 2) NOT NULL,
IsAvailable bit(1) NOT NULL, …Run Code Online (Sandbox Code Playgroud) 我有一个简单的表 - >
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
by_id INT UNSIGNED NOT NULL
posted_on INT UNSIGNED NOT NULL
Run Code Online (Sandbox Code Playgroud)
我的桌面引擎是MyISAM.
我呼吁多列索引combo1上by_id,posted_on,id
我运行这个查询 - >
EXPLAIN SELECT * FROM books
WHERE by_id = '1' AND posted_on = '0'
ORDER BY id DESC LIMIT 7;
Run Code Online (Sandbox Code Playgroud)
该Extra柱说,Using where和键列说combo1
但是,当我运行此查询 - >
EXPLAIN SELECT * FROM books
WHERE by_id IN(1,7,10) AND posted_on = '0'
ORDER BY id DESC LIMIT 7;
Run Code Online (Sandbox Code Playgroud)
该Extra …
我有一个像这样的SQL:
DBSession().query(Model).filter(***)
Run Code Online (Sandbox Code Playgroud)
我想用这个解释这个sql SQLAlchemy.
我正在尝试手动保存优化程序计划以进行进一步分析,如下所示:
do $$
declare
tmp text;
begin
explain
select * from public.some_table where 1=2 into tmp;
insert into public.plans(plan) values (tmp);
end; $$
Run Code Online (Sandbox Code Playgroud)
但是当我稍后选择它时,我看到它只从explain语句中保存了第一行:
Result (cost=0.00..82.97 rows=1 width=114)
Run Code Online (Sandbox Code Playgroud)
如何保存整个计划呢?
我有两个独立的查询,它们具有相同的输出。现在我想了解哪一个更好?
查询1:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|-------|------|---------------|--------|---------|--------|------|----------------------------------------------------|
| 1 | SIMPLE | t1 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
| 1 | SIMPLE | t2 | ALL | (null) | (null) | (null) | (null) | 9 | Using where; Using join buffer (Block Nested Loop) |
Run Code Online (Sandbox Code Playgroud)
查询2:
| id | select_type | …Run Code Online (Sandbox Code Playgroud) 下面是不使用最近创建的复合索引的更新查询/查询计划。解释表明,它不使用命名的复合索引radacctupdate,我认为这将使更新查询更快。表上还有其他索引,这些索引可供其他查询使用。
EXPLAIN UPDATE radacct SET acctstoptime = '2017-01-08 11:52:24',
acctsessiontime = unix_timestamp('2017-01-08 11:52:24') - unix_timestamp(acctstarttime),
acctterminatecause = '', acctstopdelay = 14855646
WHERE acctstoptime IS NULL AND
nasipaddress = '102.34.56.234' AND acctstarttime <= '2017-01-08 11:52:24';
************* 1. row ***********
id: 1
select_type: SIMPLE
table: radacct
type: range
possible_keys: acctstarttime,acctstoptime,nasipaddress,radacctupdate
key: nasipaddress
key_len: 17
ref: const
rows: 94
Extra: Using where; Using MRR
Run Code Online (Sandbox Code Playgroud)
下面是show index from <table>输出
+---------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name …Run Code Online (Sandbox Code Playgroud) 我有一个相当大的表,我正在积极处理和修改其中的列。describe table如果我可以按字段名称按字母顺序排序以快速找到列的定义,那就很方便了。mysql 的文档describe也没有explain提到任何有关排序的内容。我怎样才能做到这一点?
我正在分析我的查询。
postgres=# explain analyze select * from student;
QUERY PLAN
------------------------------------------------------------------------------------------------------
Seq Scan on student (cost=0.00..22.00 rows=1200 width=40) (actual time=0.005..0.005 rows=7 loops=1)
Planning time: 0.035 ms
Execution time: 0.019 ms
(3 rows)
Run Code Online (Sandbox Code Playgroud)
我不知道loop = 1意味着什么Seq Scan on student (cost=0.00..22.00 rows=1200 width=40) (actual time=0.005..0.005 rows=7 loops=1)。
我已经搜索了postgres文档,但是没有找到关于循环参数的任何很好的参考。
提前致谢。
说我有这个查询:
EXPLAIN SELECT *
FROM (
SELECT "A" as a, i.n FROM (SELECT 1 AS n) AS i
UNION ALL SELECT "B" as a, i.n FROM (SELECT 1 AS n) AS i) AS t
WHERE a = "B";
Run Code Online (Sandbox Code Playgroud)
MySQL 说
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <derived2> \N ref <auto_key0> <auto_key0> 6 const 1 100.00 \N
2 DERIVED <derived3> \N system \N \N \N \N 1 100.00 \N
3 DERIVED \N \N …Run Code Online (Sandbox Code Playgroud) explain ×10
mysql ×7
sql ×3
filesort ×2
postgresql ×2
python ×1
select ×1
sql-order-by ×1
sql-server ×1
sqlalchemy ×1
sqlite ×1
temporary ×1