Posts Tagged ‘Select’

MySQL specific shortcuts

Thursday, July 24th, 2008

MySQL provides many extentions to SQL which help performance in many common use scenarios. Among these are INSERT … SELECT, INSERT … ON DUPLICATE KEY UPDATE, and REPLACE.

I rarely hesitate to use the above since they are so convenient and provide real performance benefits in many situations. MySQL has other keywords which are more dangerous, however, and should be used sparingly. These include INSERT DELAYED, which tells MySQL that it is not important to insert the data immediately (say, e.g., in a logging situation). The problem with this is that under high load situations the insert might be delayed indefinitely, causing the insert queue to baloon. You can also give MySQL index hints about which indices to use. MySQL gets it right most of the time and when it doesn’t it is usually because of a bad scheme or poorly written query.

Mysql-Learn Your Indices

Thursday, July 24th, 2008

Often your choice of indices will make or break your database. For those who haven’t progressed this far in their database studies, an index is a sort of hash. If we issue the query SELECT * FROM users WHERE last_name = ‘Goldstein’ and last_name has no index then your DBMS must scan every row of the table and compare it to the string ‘Goldstein.’ An index is usually a B-tree (though there are other options) which speeds up this comparison considerably.

You should probably create indices for any field on which you are selecting, grouping, ordering, or joining. Obviously each index requires space proportional to the number of rows in your table, so too many indices winds up taking more memory. You also incur a performance hit on write operations, since every write now requires that the corresponding index be updated. There is a balance point which you can uncover by profiling your code. This varies from system to system and implementation to implementation.

Mysql-Use statement priorities

Thursday, July 24th, 2008

* Use INSERT LOW_PRIORITY when you want to give SELECT statements higher priority than your inserts.
* Use SELECT HIGH_PRIORITY to get retrievals that jump the queue. That is, the SELECT is executed even if there is another client waiting.

PHP:Use “sprintf”

Monday, June 30th, 2008

Example 1.Uncleanly Spliced String.

<?php

$sql = “SELECT col1, col2, col3 FROM people WHERE first_name = ‘” . mysql_real_escape_string($first_name) . “‘ AND last_name = ‘” . mysql_real_escape_string($last_name) . “‘AND foo = ‘” . ($bar = “good” ? “good” : “bad”) . “‘ ORDER BY col1″ ;

?>
I see these types of indecipherable strings –like this SQL statement example– all too frequently. The meaning of this query has been lost due to numerous concatenations and escape functions; which means developers have to invest a significant amount of time to comprehend the code.

In order to avoid this this, I use PHP’s sprintf() function. sprintf() is a function that’s part of a family of functions –referred to as the “printf” family of function– that substitute designated tokens with arguments to the function. For example, let’s look at the code in example 3.2.
Example 2: Cleanly Assembled String With sprintf()

<?php

$sql = ‘SELECT col1, col2, col3 ‘ .
‘FROM people ‘ .
‘WHERE first_name = “%s” ‘ .
‘AND last_name = “%s” ‘ .
‘AND foo = “%s” ‘ .
‘ORDER BY col1 ‘;

$sql = sprintf($sql, mysql_real_escape_string($first_name),
mysql_real_escape_string($last_name),
($bar = “good” ? “good” : “bad”));

?>

This method allows developers to regain a sense of the data that they’re representing. In this particular example the %s token means replace with a string. There a number tokens such as %d (decimal) and %f (floating point).

MYSQL:How to GRANT SELECT, INSERT, UPDATE, DELETE in MySQL

Monday, June 30th, 2008

GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO ‘user’@'%’ identified by ‘password’;

MYSQL:Building comma-separated list of strings with MySQL

Monday, June 30th, 2008

SELECT cats.id as cat_id, GROUP_CONCAT(keyword ORDER BY keyword DESC SEPARATOR ‘,’) as keywords FROM YOURPREFIX_categories cats, YOURPREFIX_keywords kws WHERE cats.id = kws.cat_id GROUP BY cats.id

MYSQL:Select/get last insert id

Monday, June 30th, 2008

$last_client = mysql_query(”select LAST_INSERT_ID()”);
// or
mysql_last_insert_id();

HTML:Highlight select html textarea contents

Monday, June 30th, 2008

<script type=”text/javascript”>
if (document.forms["form_id"] && document.forms["form_id"].data) {
document.forms["seofilter"].data.select();
}
</script>
this should be put after the form or near </body> because form may not be accessible.
In this example the textarea has name ‘data’.

Creating a PHP arrays in a HTML form

Saturday, June 28th, 2008

To get your FORM result sent as an array to your PHP script you name the INPUT, SELECT, TEXTAREA elements like this:
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyArray[]“>

If you do not specify the keys, the array gets filled in the order the elements appear in the form. Above example will contain keys 0, 1, 2 and 3. Notice the square brackets after the variable name, that’s what makes it an array. You can group the elements into different arrays by assigning the same name to c elements:
<input name=”MyArray[]“>
<input name=”MyArray[]“>
<input name=”MyOtherArray[]“>
<input name=”MyOtherArray[]“>
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It’s also possible to assign specific keys to your arrays:
<input name=”AnotherArray[]“>
<input name=”AnotherArray[]“>
<input name=”AnotherArray[email]“>
<input name=”AnotherArray[phone]“>
The AnotherArray array will now contain the keys 0, 1, email and phone.

Hide in the (Network) Neighborhood

Wednesday, June 18th, 2008

Don’t want your XP computer to show up in the network browse list (Network Neighborhood/My Network Places) to other users on your network? One way to accomplish that is to disable file sharing. To do this, click Start, right click My Network Places and select Properties. Right click your local area connection and click Properties. Uncheck the box that says File and Printer Sharing for Microsoft Networks. Click OK.

But what if you want to be able to share folders with some users; you just don’t want everyone on the network to see your computer’s shares? There’s a way:

Click Start and select Run.
In the Run box, type net config server /hidden:yes
Click OK.
Now others who know the UNC path (\\computer name\share name) can connect to your computer’s shares from the Run box, but it won’t show up in the network browse list.