如果我允许一组用户提交"explain $whatever"到mysql(通过Perl的DBI使用DBD::mysql),用户可以将任何内容放入任何数据库更改,泄漏非平凡信息,甚至导致大量数据库负载吗?如果是这样,怎么样?
我知道,通过"explain $whatever"一个可以找出表/存在的列(你要猜的名字,虽然)和大约多少条记录在一个表或多少记录有一个索引字段中的特定值.我不希望人们能够获得有关未索引字段内容的任何信息.
DBD::mysql不应该允许多个语句,所以我不希望它可以运行任何查询(只解释一个查询).只是解释,即使子查询也不应该被执行.
但我不是一个mysql专家,肯定有mysql的功能,我甚至都不知道.
在尝试提出查询计划时,优化器可能会实际执行一个表达式,以便提出索引字段将要与之进行比较的值吗?
explain select * from atable where class = somefunction(...)
Run Code Online (Sandbox Code Playgroud)
哪里atable.class是索引而不是唯一的,并且没有class='unused'找到记录但class='common'会找到一百万条记录.可能'解释'评估somefunction(...)?然后可以somefunction(...)编写这样修改数据?
我试图通过向慢查询日志中出现的查询添加索引来提高锤击wordpress DB的性能.
在MS SQL中,您可以使用查询提示强制查询使用索引,但如果正确覆盖列,则通常很容易获得查询以使用索引.
我有这个查询出现在慢查询日志中很多
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
ORDER BY wp_posts.post_date DESC
LIMIT 18310, 5;
Run Code Online (Sandbox Code Playgroud)
我在on wp_posts上创建了一个覆盖唯一索引post_date, post_status, post_type and post_id并重新启动MySQL但是当我运行解释时使用的索引是
status_password_id
Run Code Online (Sandbox Code Playgroud)
并且在可能的键中我的新索引甚至没有出现,尽管它是覆盖索引,例如我得到的
type_status_date,status_password_id
Run Code Online (Sandbox Code Playgroud)
因此,如果MySQL有一个"优化器",那么使用的索引或可能的选择都不会考虑我的post_date作为第一列的索引.我本来以为一个查询基本上是做一个TOP并按日期排序
ORDER BY wp_posts.post_date DESC LIMIT 18310, 5;
Run Code Online (Sandbox Code Playgroud)
是否希望使用按日期排序的索引来获取速度,尤其是具有满足查询所需的所有其他字段的索引?
MySQL是否有查询提示强制索引用于速度/性能测试,或者我还需要做些什么来查看为什么忽略这个索引.
如果Navicat有一个像MS SQL这样的可视化查询执行计划,我会很喜欢它,但似乎EXPLAIN是它提供的最好的.
任何有关如何强制使用索引或解决为什么被忽略的提示的人都会非常有帮助!
谢谢
我已经读过连接比子查询更好.
但
EXPLAIN QUERY PLAN
SELECT Queue.Id, NULL
FROM Queue
INNER JOIN LastQueue
ON Queue.Id=LastQueue.Id
Run Code Online (Sandbox Code Playgroud)
给
Array
(
[0] => Array
(
[selectid] => 0
[order] => 0
[from] => 0
[detail] => SCAN TABLE Queue (~1000000 rows)
)
[1] => Array
(
[selectid] => 0
[order] => 1
[from] => 1
[detail] => SEARCH TABLE LastQueue USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
)
)
Run Code Online (Sandbox Code Playgroud)
而
EXPLAIN QUERY PLAN
SELECT Queue.Id, NULL
FROM Queue
WHERE (SELECT 1 FROM LastQueue …Run Code Online (Sandbox Code Playgroud) 考虑下面的Mongo索引策略和查询,
指数:
db.collec.ensureIndex({a:1,b:1,c:1});
Run Code Online (Sandbox Code Playgroud)
查询:
db.collec.find({"a":"valueA"},{"_id":0,"a":1,"c":1}).sort({"c":-1}).limit(150)
Run Code Online (Sandbox Code Playgroud)
上述查询的解释返回:
/* 0 */
{
"cursor" : "BtreeCursor a_1_b_1_c_1",
"isMultiKey" : false,
"n" : 150,
"nscannedObjects" : 178,
"nscanned" : 178,
"nscannedObjectsAllPlans" : 279,
"nscannedAllPlans" : 279,
"scanAndOrder" : true,
"indexOnly" : true,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 1,
"indexBounds" : {
"a" : [
[
"valueA",
"valueA"
]
],
"b" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"c" : [
[
{
"$minElement" …Run Code Online (Sandbox Code Playgroud) 我试图理解PL/pgSQL函数中的select语句的查询计划,但我一直在收到错误.我的问题:如何获得查询计划?
以下是一个简单的案例,可以重现问题.
有问题的表名为test_table.
CREATE TABLE test_table
(
name character varying,
id integer
);
Run Code Online (Sandbox Code Playgroud)
功能如下:
DROP FUNCTION IF EXISTS test_function_1(INTEGER);
CREATE OR REPLACE FUNCTION test_function_1(inId INTEGER)
RETURNS TABLE(outName varchar)
AS
$$
BEGIN
-- is there a way to get the explain analyze output?
explain analyze select t.name from test_table t where t.id = inId;
-- return query select t.name from test_table t where t.id = inId;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我跑的时候
select * from test_function_1(10);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ERROR: query has no destination …Run Code Online (Sandbox Code Playgroud) 我使用mongo的explain()来检查某些查询的性能,有时keep_mutation阶段会显示如下:
"executionStats" : {
...
"executionStages" : {
"stage" : "KEEP_MUTATIONS",
"nReturned" : 1,
"executionTimeMillisEstimate" : 5460,
"works" : 79622,
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想更多地了解这个阶段,所以我通过互联网搜索,令我惊讶的是,即使在官方文档中也找不到与之相关的有用信息,更不用说其他网站了.有人可以帮忙解释一下吗?
在使用 python 和psycopg2模块时,有什么方法可以打印 SQL 执行计划中的信息以在我的终端中查看它吗?
我尝试了以下操作,但终端中没有显示任何内容:
cur.execute(cur.mogrify('explain analyze ' + sql_query, vals)
Run Code Online (Sandbox Code Playgroud)
并使用print回报None:
print(cur.execute(cur.mogrify('explain analyze ' + sql_query, vals))
Run Code Online (Sandbox Code Playgroud)
从这个问题,我也尝试了以下,但也没有奏效:
cur.execute("LOAD 'auto_explain';")
cur.execute("SET auto_explain.log_min_duration = {min_ms};".format(min_ms=0))
cur.execute(sql_query, vals)
Run Code Online (Sandbox Code Playgroud) 在 SQL Developer 中使用解释计划选项时,出现以下错误(在屏幕截图中提到)。
会话中没有未提交的事务。
即使第一次打开 SQL Developer,我也会收到此错误。
explain analyze显示 postgres 将对我的查询使用索引扫描,该查询获取行并按日期执行过滤(即2017-04-14 05:27:51.039):
explain analyze select * from tbl t where updated > '2017-04-14 05:27:51.039';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Index Scan using updated on tbl t (cost=0.43..7317.12 rows=10418 width=93) (actual time=0.011..0.515 rows=1179 loops=1)
Index Cond: (updated > '2017-04-14 05:27:51.039'::timestamp without time zone)
Planning time: 0.102 ms
Execution time: 0.720 ms
Run Code Online (Sandbox Code Playgroud)
但是运行相同的查询但使用不同的日期过滤器 '2016-04-14 05:27:51.039' 显示 postgres 将使用 seq scan 运行查询:
explain analyze select * from tbl t where updated > '2016-04-14 05:27:51.039';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Seq Scan …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚 PostgreSQL 查询执行计划是否以比会话持续时间更长的方式存储在某个地方(可能作为pg_stat_statements和的补充)。pg_prepared_statements据我所知,它PREPARE确实在 中缓存了一条 sql 语句pg_prepared_statements,尽管据我所知,该计划本身似乎在任何视图中都不可用。
我不确定是否有文档解释 PostgreSQL 查询计划的生命周期,但从EXPLAIN文档中听起来,PostgreSQL 根本不缓存查询计划。这准确吗?
谢谢!
explain ×10
postgresql ×3
sql ×3
mongodb ×2
mysql ×2
b-tree-index ×1
dbi ×1
indexing ×1
perl ×1
plpgsql ×1
psycopg2 ×1
python-3.x ×1
sqlite ×1
wordpress ×1