tags:

views:

56

answers:

3

I'm brand new to PHP & MySQL, and one function I'm creating needs to access a large table or database. I've created the database and it's currently in a MySQL table, which I'm accessing with no problem. The table is 11,000 rows in length, with 8 columns (all text less than 8 characters long) - it's static, and will never change. Without getting too particular, my users will hit a button which will trigger scripts to access the data, say 500 times or more. So in general would it be better practice to include all of this data in a big 'switch' or 'if... then' conditional right in my scripts, rather than opening and accessing the database connection hundreds, or maybe even thousands of times? It just seems like that might be a bottleneck waiting to happen. Thanks!

+1  A: 

In your case I'd keep the database but load the entire contents into memory (array) at the start of the script. If you want to optimize later on you could use a very simple lazy loading scheme, reading a batch of data on requests. Perhaps there are hints on what rows may be requested. But keep it simple!

Martin
Simple... much like myself! Thanks for the tip.
Reg H
No sweat. Please reward helpful answers by voting up and pick one of them as "the" answer. Also, edit the title to summarize the question so others may find it.
Martin
+1  A: 

In reading your question, it seems like there might be lots of room there for computational improvement. It's rare that an application must do something so many times over such a large set of data.

However, one thing to note is that a database with 11,000 rows is, in fact, rather small. The whole sum of your data should be fairly insignificant (3 bytes [max] * 8 characters * 8 fields * 11,000 rows = ~2 mb). Consider pre-loading the database into memory before your program runs, and use that instead of direct database access. It should run much faster that way.

Mike Cialowicz
It does seem like a lot of times to access the data, but it's a big lookup table for map objects - to see what the name is for particular objects north, south, east and west of a given object. I'll try what you say about loading it into memory. Thanks!
Reg H
+2  A: 

Better to eliminate that 500-times access. Why do you need it? Usually we use a database to do any calculation with a single query

Col. Shrapnel
true. I'd suggest this.
Skun