When to use views and when to use stored procedures [duplicate] - php

This question already has answers here:
MySQL: Views vs Stored Procedures
(4 answers)
Closed 9 years ago.
I am a application where number of reports are more. What i do for each report is that i create a mysql view and a mysql stored procedure. From front end php i give a call to stored procedure with where clause, based on this where clause i fetch results from the particular view. Recently i found out that it was causing performance issue. So i avoided views and wrote the same code in stored procedure and performance improved. So from that poit i am confused as to ideal situation when i should use Stored procedure and when i should use views.
And Does my scenario explained above really cause performance issue or was it problem at my end?

Views in mysql are mainly for readability. They enable you to hide a possibly complex query over multiple tables into something that appears to be a single table.
I would suspect that the most likely cause is not the use of views themselves (although not sure they would help you in any way, while a stored procedure might well be more efficient), rather a view you are using is poorly optimised (maybe ignoring keys).

Related

What are the benefits of using separate hosts for read and write in mysql (laravel)? [duplicate]

This question already has an answer here:
Master/Slave Mysql Architecture vs Server/Read DB and Separate DB for writes only [closed]
(1 answer)
Closed 3 years ago.
As laravel document says:
Sometimes you may wish to use one database connection for SELECT statements, and another for INSERT, UPDATE, and DELETE statements. Laravel makes this a breeze, and the proper connections will always be used whether you are using raw queries, the query builder, or the Eloquent ORM.
What is the benefit of doing this and what are the tradeoffs?
As far as I'm aware of, this is normally done if you have a very read-heavy application. In such a case you can have a master-slave database system which performs internal replication in order to provide more hardware power to feed the application. The slave can be a machine optimized for reading while the master is optimized for writing, although I've to admit that this goes far beyond my expertise.
There are also alternatives for load balancing SQL connections, for example using HAProxy.

Laravel - Multiple Databases at Once [duplicate]

This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed 5 years ago.
I am creating a Laravel/latest-version application which requires several hundreds of sql table to be created to serve the job. There will again be hundreds of clients who's data will be inserted multiple times (1000 rows per client or more in some tables) in these table. To eliminate confusion, I want a general/common-database for common information and separate Database for each client for specific information and would like to create new DB when a client is added. Can this be done in Laravel (dealing with multiple Databases at once) or is there any other PHP framework which can serve this purpose better? Much obliged for your help.
Well most of the times, this is an architectural flaw because the db isn't designed well. Normally this kind of issue is faced due to the reason that all your tables are normalized. Normalization is good for small applications but for data rich applications you have to judge which fields need to be normalized and which doesn't need normalization. You can save lots of tables and fields with denormalization. As a rule of thumb anything that you dont have to query directly or via join can be denormalized.
Anyways it is possible to have multiple databases attached to a single laravel application. I have done this once on a huge application.
So the idea is that before every eloquent query you will have to set the db connection.
$someModel->setConnection('mysql2');
You can find more information about it here

is it better to build sql or while loop in PHP / MySql [duplicate]

This question already has answers here:
Multiple MYSQL queries vs. Multiple php foreach loops
(5 answers)
Closed 6 years ago.
I've always used simple sql update statements inside a loop when updating multiple records. This means the sql is quick but the database is connected to multiple time.
Lately I've been wondering if it would be much better to use a loop to build up a sql statement that does multiple updates in one go.
Does it make much of a difference? I'm using PHP 5.6 & MySql.
Thank you.
The best solution would is to avoid loops altogether.
If you absolutely need to use a loop, you better do it in the database side, as this will eliminate the overhead related to the PHP/database communication.
As documented under Optimizing UPDATE Statements:
Another way to get fast updates is to delay updates and then do many updates in a row later. Performing multiple updates together is much quicker than doing one at a time if you lock the table.
You may also find the page on Optimizing INSERT Statements helpful and informative.
To summarise, in the most general terms: connecting once and sending a single UPDATE statement is better than connecting once and sending multiple UPDATE statements, which in turn is better than connecting multiple times to send a single UPDATE statement on each connection.
Beyond that, there are a host of techniques that can be harnessed to optimise further—but we would need to know the specific configuration and requirements at hand.

Localization of strings in the database [duplicate]

This question already has answers here:
Schema for a multilanguage database
(10 answers)
Closed 8 years ago.
I have an app written in PHP with MySQL in the back. Originally the app was using custom language class that was quite ineffective so I changed everything to use gettext.
The problem is a lot of strings are in the database, stored in tables that are never changed just read from. The good thing is every installation is in only one language.
In order to provide the strings in different languages, different databases were used before. There is database_en, database_fr, etc. and every new installation is using the appropriate localized database. I want to change this - one database for all languages.
I am thinking of moving everything language specific from the database to config files where gettext can be used again.
Do you think this is good idea or you would continue using different databases for different installations? Can you recommend something better ?
I recommend using google translation api at front end and single database for all with unicode data types.

How can I insert data into two mysql tables at once using PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
mySQL - Insert into three tables
I want to know if I can insert data into two tables using one query in PHP/MySQL?
If it's possible, can I see an example?
No, it is not possible. Mysql INSERT syntax accepts one and only one table as a target
using one query
no
Using one query! I don't think so.
However, I believe that the most appropriate methodology is to utilise transactions. Using transactions, you assure to have data inserted in more than one table (successfully), otherwise any changes are rolled back. That is, either persist all changes or do nothing at all.

Categories