You shouldn't be seeing a perceptible performance difference from preparing a statement compared to having one hard-coded into your stored procedure. You also should see no difference at all from the fact that the procedures are stored in a different database from your data. From the start, though you're presenting us with two different unknowns -- different schema and dynamic SQL. The best way to troubleshoot this is to sectionalize the problem down to the individual variables... by putting a procedure with static (not dynamic) in a different schema from the data (shouldn't matter) or by putting a procedure with a dynamic query in the same schema as the data (also shouldn't matter). But I suspect the problem is related to neither factor. I think it's the dynamic queries you're building. You may actually be getting wrong results, in addition to your queries taking longer to run. You appear to have You could replace 1 in the body of your prepared statement, and then, before the actual 2, assign the values of those session ("@") variables to be the same as the procedure variables:
Note that this doesn't put the contents of the variables into your query, either -- concatenating the contents of variables from outside your procedure into dynamic sql almost always is a bad (insecure) idea -- but this doesn't do that. It puts the variable names into the statement, which is safe. I think this is the solution to your problem, if the question accurately represents the dynamic sql you're building. However, I have a further recommendation, in the interest of security and good practice. I mentioned never concatenating external variables into dynamic sql... but you can easily sanitize the variable 3, making it safe to concatenate in.Replace this:
with this:
This scalar subquery will return only two possible things: it will always return either the same value passed in as 3 if that database really does exist; otherwise it will return 5. This will make the generated SQL throw an error if an invalid database name is provided, rather than potentially executing arbitary sql code slipped into your dynamic query string via the db_name variable and executed with the permissions of the procedure definer. Dalam membuat aplikasi database MySQL 5, kita bisa meletakkan fungsi-fungsi yang merupakan bisnis prosesnya dengan 2 pilihan
Tabel Sales Order CREATE TABLE sales_order( Hasilnya bisa dilihat di phpmyadmin : Tabel Sales Item CREATE TABLE sales_item( INSERT INTO sales_item(so_id,item_name,item_price,hpp) VALUES (1, ‘Keyboard’, 100000,85000); Hasilnya : Store Procedure Resume Sales Order Untuk membuatnya pastikan delimiternya // Menjalankan Store Procedure Di sini terlihat total_pricenya berubah dari 0 menjadi 150000, total_hpp menjadi 12500 dan profit menjadi 25000. Langkah berikutnya adalah membuat Trigger di MySQL 5 . Kunjungi www.proweb.co.id untuk menambah wawasan anda. Store Procedure di MySQL 5 |