小编The*_*uhn的帖子

Postgres中的快速近似计数

我正在查询我的数据库(Postgres 8.4),如下所示:

SELECT COUNT(*) FROM table WHERE indexed_varchar LIKE 'bar%';
Run Code Online (Sandbox Code Playgroud)

这种复杂性是O(N),因为Postgres必须计算每一行.Postgres 9.2具有仅索引扫描,但不幸的是升级不是一种选择.

但是,获得精确的行数看起来有点过分,因为我只需要知道以下三种情况中的哪一种是真的:

  • 查询不返回任何行.
  • 查询返回一行.
  • 查询返回两行或更多行.

所以我不需要知道查询返回10,421行,只是它返回两个以上.

我知道如何处理前两种情况:

SELECT EXISTS (SELECT COUNT(*) FROM table WHERE indexed_varchar LIKE 'bar%');
Run Code Online (Sandbox Code Playgroud)

如果存在一行或多行并且false不存在,则返回true.

有关如何扩展这一点以便有效地涵盖所有三种情况的任何想法?

sql postgresql

4
推荐指数
1
解决办法
1047
查看次数

找到我的Postgres文本搜索词典

几个月前我创建了一个全文搜索词库.我刚刚添加了一些条目,并且(我认为)我这样更新:

ALTER TEXT SEARCH CONFIGURATION english
    ALTER MAPPING FOR asciiword, asciihword, hword_asciipart
    WITH [my_thesaurus], english_stem;
Run Code Online (Sandbox Code Playgroud)

但是,我实际上并不记得我的词库被称为什么.我怎么能搞清楚这一点?

postgresql

4
推荐指数
1
解决办法
1868
查看次数

如何使用 ProcessPoolExecutor 优雅地退出程序?

以下面的程序为例:

import asyncio
from concurrent.futures import ProcessPoolExecutor


def process():
    print('processed')

async def main(loop, executor):
    await loop.run_in_executor(executor, process)
    await asyncio.sleep(60.0)

executor = ProcessPoolExecutor()
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main(loop, executor))
except KeyboardInterrupt:
    pass
finally:
    executor.shutdown()
Run Code Online (Sandbox Code Playgroud)

如果我在程序运行时点击Ctrl+ C,我会在它退出时得到一个真正的消息回溯:

processed
^CProcess Process-3:
Process Process-4:
Process Process-2:
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/pytho
n3.5/multiprocessing/process.py", line 254, in _bootstrap
    self.run()
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/pytho
n3.5/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/pytho
n3.5/concurrent/futures/process.py", line 169, in _process_worker
    call_item = call_queue.get(block=True)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/pytho
n3.5/multiprocessing/queues.py", …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

4
推荐指数
1
解决办法
4310
查看次数

PHP:发现一天是否存在于该月的最后一周

客户希望每周一自动生成时事通讯,显示即将到来的一周的时间表.这很简单:

if(date('N', $time)==1) { /* Stuff */ }
Run Code Online (Sandbox Code Playgroud)

将它附加到每晚运行的crontab上,我很高兴.

但是,如果在该月的最后一周生成时事通讯,则需要显示下个月的时间表.我如何确定何时需要生成月度计划?

php datetime

2
推荐指数
1
解决办法
923
查看次数

标签 统计

postgresql ×2

datetime ×1

php ×1

python ×1

python-3.x ×1

python-asyncio ×1

sql ×1