Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Massive PHP array vs MySQL Database?

I'm debating in my head whether I should use a massive, multidimensional array or a database in MySQL. I'm developing for a client whose business has many products. In this multidimensional array I would include the product title, description, image link and categories for each individual product.

My client has perhaps 1000+ products. I've researched other similar questions and many of them say that an array is perhaps faster, however none of them are dealing with an array of this scale.

I personally would much rather use an array, because my knowledge with MySQL is extremely limited, but if it means sacrificing a significant amount of speed then I would rather use the database. Which would you consider a more appropriate option for my case?

like image 967
Franco Selem Avatar asked Sep 09 '13 20:09

Franco Selem


People also ask

Which is faster PHP or MySQL?

MySQL is faster in scope of SQL query. PHP is faster in PHP code. If you make SQL query to find out SQRT() it should be definitely slower (unless PHP is broken) because MySQL parser and networking overhead.

Is MySQL good for large database?

MySQL is a widely used open-source relational database management system (RDBMS) and an excellent solution for many applications, including web-scale applications. But the MySQL architecture has limitations when it comes to big data analytics.

Which is better MySQL or PHP?

PHP is a fast and feature-rich open source scripting language used to develop Web Applications or Internet / Intranet Applications. MySQL is a powerful open source database server built based on a relational database management system (RDBMS) and is capable of handling a large concurrent database connection.

Which is faster PHP or SQL?

If you are fetching data from SQL and then doing the calculations in PHP over the fetched data its far better to fetch the required result and avoid PHP processing. The time taken to fetch the data in SQL is time consuming but once its done calculations are more over same.


1 Answers

The other answers are right - but for the wrong reasons.

Holding the data in a PHP array it will be much faster to fetch than from a database - even if the dataset is cached in memory. The issue is that in normal PHP architectures each request is handled by a seperate process. Hence each request which needs access to the data will have to load the entire dataset into memory. This takes time. The point at which it becomes more expensive to perform this operation rather than retrieving items from a database depends on a lot of different factors - but as a rough rule of thumb it's in the region of 100 records. There are applications where this model makes sense - but they rely on very small data volumes and a controlled process of changing / managing the data.

Your next problem is that you'll probably want to record some transactions against the stock - that means changing the data - and that means serializing access to ensure that 2 separate transactions don't happen at the same time. It is impossible to implement this in PHP (without a dedicated daemon to adjudicate) while not having deadlocks.

If you are charging someone for implementing the code, then it should be blindingly obvious that trying to implement this in memory is a very, very bad idea.

like image 149
symcbean Avatar answered Oct 03 '22 14:10

symcbean