Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL 1,000's of OR Statements (Good Idea?)

Tags:

sql

mysql

I'm building a Twitter application that grabs a users entire following and gets their specific id Ex: 1223455

I also have a huge database full of rows that contain a specific Twitter id... Look at the examples in rows...

|1| 122345   |
|2| 2232144  |
|3| 99653222 |
|4| 123232   |
|5| 2321323  |
|6| 3121322  |

The problem is we all know that Twitter is all about more and more followers (1,000's), and I was wonder is this is a good MySQL query to run potentially up to 20 times in one script run...

SELECT * FROM table WHERE twitterID='132323' OR twitterId='23123' OR twitterId='23123' OR twitterId='23123' OR twitterId='23123' OR twitterId='23123' OR twitterId='23123' OR twitterId='23123

And on and on and on and on... There could potentially be over 1,000 OR statements in a single query (and a similar query like this could be called 20 times)

This doesn't seem like a very good programming practice, but I haven't heard of another way...??

like image 708
The Man Avatar asked Jul 29 '12 00:07

The Man


People also ask

Can MySQL handle 100 billions of records?

Can MySQL handle 100 million records? Yeah, it can handle billions of records. If you properly index tables, they fit in memory and your queries are written properly then it shouldn't be an issue.

Can MySQL handle billions of rows?

The largest table we had was literally over a billion rows. This was using MySQL 5.0, so it's possible that things may have improved. It worked. MySQL processed the data correctly most of the time.

Can MySQL handle millions of records?

Millions of rows is fine, tens of millions of rows is fine - provided you've got an even remotely decent server, i.e. a few Gbs of RAM, plenty disk space. You will need to learn about indexes for fast retrieval, but in terms of MySQL being able to handle it, no problem. Save this answer. Show activity on this post.

How much data can MySQL handle?

The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, even if the storage engine is capable of supporting larger rows. BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row.


2 Answers

Use the in specifier:

Select * from table where twitterid in (123,456,789,...)
like image 162
Mike Mackintosh Avatar answered Oct 02 '22 04:10

Mike Mackintosh


This is a very bad idea indeed. MySQL doesn't do a good job with optimizing OR statements.

You'd be far better off using a JOIN or something like this

WHERE twitterId IN (
      select * 
        from followers
       where followee = whatever
)
like image 31
O. Jones Avatar answered Oct 02 '22 05:10

O. Jones