Fastest way to find not null filed in sqlite

匆匆过客 提交于 2019-12-12 01:38:48

问题


I have sqlite table

CREATE TABLE IF NOT EXISTS [app_status](
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
[status] TEXT DEFAULT NULL
) 

This table is having multiple records like

1 "success"
2 NULL

where NULL is sqlite NULL

What is the fastest way to find out if table at-least one row where status IS NOT NULL? Can I create some index or something else which I can use to count Not NULL fields ?

I have written following query

SELECT 1 \
FROM [app_status]\
WHERE [status] IS NOT NULL

But it is taking 3 ms to 50 ms. I want to further optimize this time. How can I do that ?


回答1:


Add an index on that column if it doesn't already have one, and/or limit your select.

select 1 from [app_status] where [status] is not null limit 1;

You don't need to go through the whole table if you're just checking if the column contains at least one non-null field.



来源:https://stackoverflow.com/questions/9484748/fastest-way-to-find-not-null-filed-in-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!