The fastest way to check if some records in a database table?

前端 未结 6 1444
耶瑟儿~
耶瑟儿~ 2020-12-14 22:44

I have a huge table to work with . I want to check if there are some records whose parent_id equals my passing value . currently what I implement this is by using \"select

6条回答
  •  庸人自扰
    2020-12-14 23:31

    First of all, you need an index on mytable.parent_id.

    That should make your query fast enough, even for big tables (unless there are also a lot of rows with the same parent_id).

    If not, you could write

    select 1 from mytable where parent_id = :id and rownum < 2
    

    which would return a single row containing 1, or no row at all. It does not need to count the rows, just find one and then quit. But this is Oracle-specific SQL (because of rownum), and you should rather not.

提交回复
热议问题