44693

Stopping MySQL query after the first row [closed]

Question:

I have a piece of MySQL code that searches through a quite lengthy mysql database. When done multiple times, it takes a long time, although the only thing I need from the code is to check the existence of at least one entry. Is it possible to instruct the mysql code to stop searching after finding one row? Thanks.

Answer1:

Try LIMIT 1 at the end of your query.

Answer2:

if you are trying to search for just one thing then there must be some primary key associated with it. so in the query you can include somethere in where clause which will bring in only one result. something like this

select * from students where id='123'

As primary keys are the unique key so there won't be any duplication and only one search will be done.

Answer3:

SELECT * FROM tablename LIMIT 1 OFFSET 0 <ul><li>LIMIT will set the max items you want back</li> <li>OFFSET let's you state the starting point to start your query</li> </ul>

Combining LIMIT and OFFSET gives you very precise control of fetching tons of database rows over multiple sessions.

Or use the easier 2-parameter LIMIT function (same as LIMIT, OFFSET):

SELECT * FROM tablename LIMIT 1, 0

Recommend

  • Solrj indexing mechanism
  • Solrj indexing mechanism
  • Disable generics validation in Eclipse via M2E
  • How do I use DisplayFor() for objects that are not directly part of my model in asp .net mvc 2?
  • what does the follow prolog codes do?
  • SQL Server Like Query not case sensitive
  • Does sql server minds the way records where inserted?
  • SQL append distinct values from two columns and two tables
  • RabbitMQ java client stops consuming messages
  • Get UILabel out of UIButton
  • command line of process by name
  • SQL Server re-calculate or not?
  • Does the MySQL IN clause execute the subquery multiple times?
  • Unix Network Programming Clarification
  • Django model for a Postgres view
  • Which open source license has no forking [closed]
  • How can I tell a form not to dispose a particular control when it closes?
  • git add error : “fatal : malloc, out of memory”
  • SonarQube: Cannot deactivate rule with missing quality profile
  • UWP/C# - Issue with AQS and USB Devices
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • one Local Olampyad Questions on Informatic in 2011
  • Meteor helpers not available in Angular template
  • Extracting HTML between tags
  • Using $this when not in object context
  • javaw.exe and eclipse startup problems
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Trying to switch camera back to front but getting exception
  • How to limit post in wp_query
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Delete MySQLi record without showing the id in the URL
  • What do the 'size' numbers mean in the windbg !heap output?
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • Can't mass-assign protected attributes when import data from csv file
  • Unable to use reactive element in my shiny app