SELECT EXISTS vs. LIMIT 1

2024/10/6 18:25:31

I see SELECT EXISTS used a lot like:

    if db.query("""SELECT EXISTS (SELECT 1 FROM checkoutWHERE checkout_id = %s)""" % checkout_id).getresult()[0][0] == 't':

vs. what i prefer:

    if db.query("""SELECT 1 FROM checkoutWHERE checkout_id = %sLIMIT 1""" % checkout_id).getresult():

Which one is preferred and why?

P.S. i am using Python and PosgreSQL.

cert=> explain SELECT EXISTS (SELECT 1 FROM checkout WHERE checkout_id = 3);QUERY PLAN                                      
--------------------------------------------------------------------------------------Result  (cost=4.03..4.03 rows=1 width=0)InitPlan->  Index Scan using checkout_pkey on checkout  (cost=0.00..4.03 rows=1 width=0)Index Cond: (checkout_id = 3)
(4 rows)cert=> explain SELECT 1 FROM checkout WHERE checkout_id = 3 limit 1;QUERY PLAN                                     
------------------------------------------------------------------------------------Limit  (cost=0.00..4.03 rows=1 width=0)->  Index Scan using checkout_pkey on checkout  (cost=0.00..4.03 rows=1 width=0)Index Cond: (checkout_id = 3)
(3 rows)

My point is, why getting a row from the result and check if it's first column is true, if i can just check if there are any rows at all, meaning the same?

Answer

To my eye the second statement is problematic in that it will not return a row if the condition is not satisfied.

https://en.xdnf.cn/q/70336.html

Related Q&A

How to access a specific start_url in a Scrapy CrawlSpider?

Im using Scrapy, in particular Scrapys CrawlSpider class to scrape web links which contain certain keywords. I have a pretty long start_urls list which gets its entries from a SQLite database which is …

Escaping search queries for Googles full text search service

This is a cross-post of https://groups.google.com/d/topic/google-appengine/97LY3Yfd_14/discussionIm working with the new full text search service in gae 1.6.6 and Im having trouble figuring out how to …

dificulty solving a code in O(logn)

I wrote a function that gets as an input a list of unique ints in order,(from small to big). Im supposed to find in the list an index that matches the value in the index. for example if L[2]==2 the out…

Scrapy. How to change spider settings after start crawling?

I cant change spider settings in parse method. But it is definitely must be a way. For example:class SomeSpider(BaseSpider):name = mySpiderallowed_domains = [example.com]start_urls = [http://example.co…

numpy ctypes dynamic module does not define init function error if not recompiled each time

sorry for yet an other question about dynamic module does not define init function. I did go through older questions but I didnt find one which adress my case specifically enought.I have a C++ library …

How do I save Excel Sheet as HTML in Python?

Im working with this library XlsxWriter.Ive opened a workbook and written some stuff in it (considering the official example) - import xlsxwriter# Create a workbook and add a worksheet. workbook = xlsx…

Faster sockets in Python

I have a client written in Python for a server, which functions through LAN. Some part of the algorithm uses socket reading intensively and it is executing about 3-6 times slower, than almost the same …

Python gmail api send email with attachment pdf all blank

I am using python 3.5 and below code is mostly from the google api page... https://developers.google.com/gmail/api/guides/sending slightly revised for python 3.xi could successfully send out the email …

how to find height and width of image for FileField Django

How to find height and width of image if our model is defined as followclass MModel:document = FileField()format_type = CharField()and image is saved in document then how we can find height and width o…

Given a pickle dump in python how to I determine the used protocol?

Assume that I have a pickle dump - either as a file or just as a string - how can I determine the protocol that was used to create the pickle dump automatically? And if so, do I need to read the entir…