小编rcr*_*ers的帖子

ruby - 为什么不阻止像方法一样继承其调用者的$ SAFE级别?

当$ SAFE = 4的线程调用方法时,该方法以相同的$ SAFE级别运行:

def test_method
  raise "value of $SAFE inside the method: #{$SAFE}"
end
t = Thread.new{$SAFE = 4; self.test_method}; t.join
 => RuntimeError: value of $SAFE inside the method: 4
Run Code Online (Sandbox Code Playgroud)

但是,当调用块时,它似乎使用原始上下文中的$ SAFE:

test_lambda = lambda do
  raise "value of $SAFE inside the lambda: #{$SAFE}"
end
t = Thread.new{$SAFE = 4; test_lambda.call}; t.join
 => RuntimeError: value of $SAFE inside the lambda: 0
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么它这样工作?这似乎是一个安全问题.

(我使用的原因raise,而不是puts在于puts没有在$ SAFE = 4工作)

这可用于在看似安全的上下文中评估受污染的字符串:

test_lambda = lambda{|s| puts "Tainted: …
Run Code Online (Sandbox Code Playgroud)

ruby scope global-variables block

7
推荐指数
1
解决办法
276
查看次数

为什么LIMIT 2在此查询中的数量级比LIMIT 1长?

我在PostgreSQL 9.3上.这应该在任何超过100,000行的表上重现.EXPLAIN ANALYZE显示使用LIMIT 2扫描更多行,但我无法弄清楚原因.

限制1:

EXPLAIN ANALYZE WITH base AS (
  SELECT *, ROW_NUMBER() OVER () AS rownum FROM a_big_table
), filter AS (
  SELECT rownum, true AS thing FROM base
) SELECT * FROM base LEFT JOIN filter USING (rownum) WHERE filter.thing LIMIT 1
Run Code Online (Sandbox Code Playgroud)

结果:

Limit  (cost=283512.19..283517.66 rows=1 width=2114) (actual time=0.019..0.019 rows=1 loops=1)
  CTE base
    ->  WindowAgg  (cost=0.00..188702.69 rows=4740475 width=101) (actual time=0.008..0.008 rows=1 loops=1)
          ->  Seq Scan on a_big_table  (cost=0.00..129446.75 rows=4740475 width=101) (actual time=0.003..0.003 rows=1 loops=1)
  CTE filter
    -> …
Run Code Online (Sandbox Code Playgroud)

sql postgresql join common-table-expression window-functions

5
推荐指数
1
解决办法
103
查看次数

Postgres选择慢得多的Seq Scan,而不是Index Scan

首先,请确保计划者已更新统计信息:

my_db=> vacuum analyze;
VACUUM
Time: 1401.958 ms
Run Code Online (Sandbox Code Playgroud)

仅选择时foos.bar_id,在该列上执行“仅索引扫描”即可正常执行查询:

my_db=> EXPLAIN ANALYZE SELECT foos.bar_id FROM foos INNER JOIN bar_ids ON foos.bar_id = bar_ids.id;
 QUERY PLAN                                                                            
 Nested Loop  (cost=0.43..16203.46 rows=353198 width=4) (actual time=0.045..114.746 rows=196205 loops=1)
   ->  Seq Scan on bar_ids  (cost=0.00..16.71 rows=871 width=4) (actual time=0.005..0.195 rows=871 loops=1)
   ->  Index Only Scan using index_foos_on_bar_id on foos  (cost=0.43..14.80 rows=378 width=4) (actual time=0.003..0.055 rows=225 loops=871)
         Index Cond: (bar_id = bar_ids.id)
         Heap Fetches: 0
 Planning time: 0.209 ms
 Execution time: 144.364 ms
(7 …
Run Code Online (Sandbox Code Playgroud)

postgresql indexing join

5
推荐指数
1
解决办法
2266
查看次数