Archive for the 'mysql' Category
optimizing mysql tables
By Justin Silverton
Many times, slow access to a mysql database can be the result of Badly defined or non-existent indexes and fixing these can often lead to better performance. Here is an
example table:
CREATE TABLE address_book (
contact_number char(10) NOT NULL,
firstname varchar(40),
surname varchar(40),
address text,
telephone varchar(25)
);
example query: SELECT firstname FROM address_book WHERE contact_number = ‘12312′;
This will retrieve the firstname of a person added to the address_book table, based on the contact number.
Without any kind of indexes added to this table, mysql will have to search through each row to find the item that you would like to find, which is very inefficient.
Optimizing your table
There is a built-in command called explain, that can show you what, if any, indexes that are being used to retrieve results.
example:
EXPLAIN SELECT firstname FROM address_book WHERE contact_number = ‘12312′;
This will return a set of results that will tell you how myql is processing the results
table: The table the output is about (will show multiple if you have joins)
type: The type of join is being used.best to worst the types are: system, const, eq_ref, ref, range, index, all
possible_keys: Shows which possible indexes apply to this table
key: And which one is actually used
key_len: The length of the key used. The shorter that better.
ref: The column, or a constant, is used
rows: The number of rows mysql believes it must examine to get the data
extra: You don’t want to see “using temporary” or “using filesort”
and index can be added to the above example table using the following command:
ALTER TABLE address_book ADD INDEX(contact_number);
you can also add an index on only part of a varchar. In the following, I will add an index on only 8 of the 10 characters.
ALTER TABLE address_book ADD INDEX(contact_number(8));
Why would you want to do this?
Indexes do increase performance in the right situations, but they are also a tradeoff between speed and space. The bigger an index is, the more space it will consume on your harddrive.
Using the query optimizer/analyzer
the following command can analyze your table key distribution to find out the best indexes to use:
analyze table *tablename*
also, another thing to keep in mind is the fact that over time, update and delete operations leave gaps in the table, which will cause un-needed overhead when reading data from your tables.
from time to time, it is a good idea to run the following (which will fix the above issue):
optimize table *tablename*
No comments5 mysql tips
By Justin Silverton
These are some tips that may help you out when dealing with mysql tables (known in 4.1 and below).
1) char and varchar are case sensitive
example:
if you have a table that contains the following:
table newtable (
name varchar(32)
)
name contains the name “John Smith”.
the following statement: “SELECT * from newtable where name=’john smith’ will return our record.
to stop this from happening, use the following when you create your table:
CREATE TABLE newtable (
name VARCHAR(32) BINARY
)
2) Varchar type is limited to 255 characters
3) Varchar trailing spaces are stripped
example: insert into newtable values(’Test with no spaces ‘);
select concat(name, ‘no spaces’) FROM newtable;
output will be: Test with no spacesnospaces
Varchar works this way, because it saves space by stripping the spaces.
if you need to keep the trailing spaces in the data you are adding to a varchar type,
you need to use the text or blob types.
4) operator
The (or) operater is a logical operator
example: select ’string1′ ’string2′ will not return ’string1string2′
5) function parameters
This issue has caused me many headaches in the past, and I am not sure why this issue was never fixed. If there is a space
between the paramater list and an internal function that you want to execute, it will return an error.
example: select min (my_field) from mytable wil return an error, while select min(my_field) from my_table will not
1 commentanother article on oracle/mysql
“In November, I looked at Oracle’s purchase of InnoDB, as well as their release of Oracle Express, and the effect on MySQL. In short, I concluded that the positives for Oracle were clear, but that MySQL must be feeling outmaneuvered. Since InnoDB, with its full transactional capability, has become an integral part of their product offering, I suggested that both BerkleyDB (their first transactional storage engine, but which has never got beyond so-called gamma status) and MaxDB (what was SAPDB, which is a fully-featured database but does not yet integrate well with MySQL’s other products) had both become critically important.
Shortly after Oracle purchased InnoDB, MySQL vice-president Richard Mason acknowledged that MySQL were “evaluating options to replace that functionality in some way,” but that they were “not at the point yet where we can go public with what that plan is but we will be shortly.”
However, since then Oracle has upped the ante even more. On February 14th, Oracle purchased Sleepycat Software, who provides MySQL with the Berkeley DB transactional storage engine. Furthermore, in the last few days, rumors have been flying that Oracle also intends to purchase both Zend (’the PHP Company’), as well as JBoss. I believe this is another smart series of moves by Oracle that can only benefit them……”
The rest can be found Here
using java and mysql
by Paul DuBois
You can write MySQL applications in a variety of languages. The languages that most people use with MySQL are PHP and Perl, but a sometimes overlooked option is the MySQL Connector/J driver, which allows you to develop Java applications that interact with your MySQL server.
MySQL Connector/J works within the framework of the Java JDBC interface, an API that allows Java programs to use database servers in a portable way. JDBC is based on an approach similar to that used in the design of Perl and Ruby DBI modules, Python’s DB-API module, and
PHP’s PEAR::DB class. This approach uses a two-tier architecture:
The top level is visible to application programs and presents an abstract interface for connecting to and using database engines. The application interface does not depend on details specific to particular engines.
The lower level consists of drivers for individual database engines. Each driver handles the details necessary to map the abstract application interface onto operations that a specific engine will understand.The JDBC interface allows developers to write applications that can be used with different databases with a minimum of porting effort. Once a driver for a given server engine is installed, JDBC applications can communicate with any server of that type. By using MySQL Connector/J, your Java programs can access MySQL databases.
Note: MySQL Connector/J is the successor to the MM.MySQL driver. If you have JDBC programs written for MM.MySQL, they should work with MySQL Connector/J as well, although you may want to update the driver class name used in your programs. Just replace instances of org.gjt.mm.mysql in your Java source files with com.mysql.jdbc and recompile.
Preliminary Requirements
To use Java applications with MySQL, you may need to install some additional software:
If you want to compile and run Java programs, you’ll need a Java compiler (such as javac or jikes) and a runtime environment. If these are not already installed on your system, you can get them by obtaining a Java Software Development Kit (SDK) from java.sun.com.
If you want only to run precompiled applications, no compiler is necessary, but you’ll still need a Java Runtime Environment (JRE). This too may be obtained from java.sun.com.This article assumes that you’ll write and compile your own programs, and thus that you have a Java SDK installed. Once you compile a Java program, however, you can deploy it to other machines, even ones that have only a runtime environment. This works even in heterogenous installations, because Java is platform-independent. Applications compiled on one platform can be expected to work on other platforms. For example, you can develop on a Linux box and deploy on Windows
Connecting to the MySQL Server
To connect to the MySQL server, register the JDBC driver you plan to use, then invoke its getConnection() method. The following short program, Connect.java, shows how to connect to and disconnect from a server running on the local host. It accesses a database named test, using a MySQL account with a user name and password of testuser and testpass: import java.sql.*;
public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = “testuser”;
String password = “testpass”;
String url = “jdbc:mysql://localhost/test”;
Class.forName (”com.mysql.jdbc.Driver”).newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println (”Database connection established”);
}
catch (Exception e)
{
System.err.println (”Cannot connect to database server”);
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println (”Database connection terminated”);
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}
Compile Connect.java to produce a class file Connect.class that contains executable Java code: % javac Connect.java
Then invoke the class file as follows and it should connect to and disconnect from your MySQL server: % java Connect
Database connection established
Database connection terminated
If you have trouble compiling Connect.java, double check that you have a Java Software Development Kit installed and make sure that the MySQL Connector/J driver is listed in your CLASSPATH environment variable.
The arguments to getConnection() are the connection URL and the user name and password of
a MySQL account. As illustrated by Connect.java, JDBC URLs for MySQL consist of jdbc:mysql:// followed by the name of the MySQL server host and the database name. An alternate syntax for specifying the user and password is to add them as parameters to the end of the connection URL: jdbc:mysql://localhost/test?user=testuser&password=testpass
When you specify a URL using this second format, getConnection() requires only one argument.
For example, the code for connecting to the MySQL server in Connect.java could have been written like this: String userName = “testuser”;
String password = “testpass”;
String url = “jdbc:mysql://localhost/test?user=”
+ userName
+ “&password=”
+ password;
Class.forName (”com.mysql.jdbc.Driver”).newInstance ();
conn = DriverManager.getConnection (url);
getConnect() returns a Connection object that may be used to interact with MySQL by issuing queries and retrieving their results. (The next section describes how to do this.) When you’re done with the connection, invoke its close() method to disconnect from the MySQL server.
To increase the portability of your applications, you can store the connection parameters (host, database, user name, and password) in a Java properties file and read the properties at runtime. Then they need not be listed in the program itself. This allows you to change the server to which the program connects by editing the properties file, rather than by having to recompile the program.
Issuing Queries
To process SQL statements in a JDBC-based application, create a Statement object from your Connection object. Statement objects support an executeUpdate() method for issuing queries that modify the database and return no result set, and an executeQuery() method for queries that do return a result set. The query-processing examples in this article use the following table, animal, which contains an integer id column and two string columns, name and category: CREATE TABLE animal
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
name CHAR(40),
category CHAR(40)
)
id is an AUTO_INCREMENT column, so MySQL automatically assigns successive values 1, 2, 3, … as records are added to the table.
Issuing Queries That Return No Result Set
The following example obtains a Statement object from the Connection object, then uses it to create and populate the animal table. DROP TABLE, CREATE TABLE, and INSERT all are statements that modify the database, so executeUpdate() is the appropriate method for issuing them:
Statement s = conn.createStatement ();
int count;
s.executeUpdate (”DROP TABLE IF EXISTS animal”);
s.executeUpdate (
“CREATE TABLE animal (”
+ “id INT UNSIGNED NOT NULL AUTO_INCREMENT,”
+ “PRIMARY KEY (id),”
+ “name CHAR(40), category CHAR(40))”);
count = s.executeUpdate (
“INSERT INTO animal (name, category)”
+ ” VALUES”
+ “(’snake’, ‘reptile’),”
+ “(’frog’, ‘amphibian’),”
+ “(’tuna’, ‘fish’),”
+ “(’racoon’, ‘mammal’)”);
s.close ();
System.out.println (count + ” rows were inserted”);
The executeUpdate() method returns the number of rows affected by a query. As shown above, the count is used to report how many rows the INSERT statement added to the animal table.
A Statement object may be used to issue several queries. When you’re done with it, invoke its close() method to dispose of the object and free any resources associated with it
Issuing Queries That Return a Result Set
For statements such as SELECT queries that retrieve information from the database, use executeQuery(). After calling this method, create a ResultSet object and use it to iterate through the rows returned by your query. The following example shows one way to retrieve the contents of the animal table:
Statement s = conn.createStatement ();
s.executeQuery (”SELECT id, name, category FROM animal”);
ResultSet rs = s.getResultSet ();
int count = 0;
while (rs.next ())
{
int idVal = rs.getInt (”id”);
String nameVal = rs.getString (”name”);
String catVal = rs.getString (”category”);
System.out.println (
“id = ” + idVal
+ “, name = ” + nameVal
+ “, category = ” + catVal);
++count;
}
rs.close ();
s.close ();
System.out.println (count + ” rows were retrieved”);
executeQuery() does not return a row count, so if you want to know how many rows a result set contains, you should count them yourself as you fetch them.
To obtain the column values from each row, invoke getXXX() methods that match the column data types. The getInt() and getString() methods used in the preceding example return integer and string values. As the example shows, these methods may be called using the name of a result set column. You can also fetch values by position. For the result set retrieved by the SELECT query in the example, id, name, and category are at column positions 1, 2 and 3 and thus could have been obtained like this:
int idVal = rs.getInt (1);
String nameVal = rs.getString (2);
String catVal = rs.getString (3);
ResultSet objects, like Statement objects, should be closed when you’re done with them.
To check whether or not a column value is NULL, invoke the result set object’s wasNull() method after fetching the value. For example, you could check for a NULL value in the name column like this: String nameVal = rs.getString (”name”);
if (rs.wasNull ())
nameVal = “(no name available)”;
Using Placeholders
Sometimes it’s necessary to construct queries from values containing characters that require special treatment. For example, in queries, string values are written enclosed within quotes, but any quote characters in the string itself should be doubled or escaped with a backslash to avoid creating malformed SQL. In this case, it’s much easier to let JDBC handle the escaping for you, rather than fooling around trying to do so yourself. To use this approach, create a different kind of statement (a PreparedStatement), and refer to the data values in the query string by means of placeholder characters. Then tell JDBC to bind the data values to the placeholders and it will handle any special characters automatically.
Suppose you have two variables nameVal and catVal from which you want to create a new record in the animal table. To do so without regard to whether or not the values contain special characters, issue the query like this:
PreparedStatement s;
s = conn.prepareStatement (
“INSERT INTO animal (name, category) VALUES(?,?)”);
s.setString (1, nameVal);
s.setString (2, catVal);
int count = s.executeUpdate ();
s.close ();
System.out.println (count + ” rows were inserted”);
The ‘?’ characters in the query string act as placeholders–special markers indicating where data values should be placed. The setString() method takes a placeholder position and a string value and binds the value to the appropriate placeholder, performing any special-character escaping that may be necessary. The method you use to bind a value depends on the data type. For example, setString() binds string values and setInt() binds integer values.
Error Handling
If you want to trap errors, execute your JDBC operations within a try block and use an exception handler to display information about the cause of any problems that occur. JDBC provides getMessage() and getErrorCode() methods that may be invoked when an exception occurs to obtain the error message and the numeric error code. The following example deliberately issues a malformed query. When it runs, the executeQuery() method fails and raises an exception that is handled in the catch block: try
{
Statement s = conn.createStatement ();
s.executeQuery (”XYZ”); // issue invalid query
s.close ();
}
catch (SQLException e)
{
System.err.println (”Error message: ” + e.getMessage ());
System.err.println (”Error number: ” + e.getErrorCode ());
}
ezsql 2.0 released
Overview
ezSQL is a class that makes it ridiculously easy to use mySQL, Oracle8, SQLite (PHP), within your PHP script. Includes lots of examples making it very easy to understand how to work with databases. ezSQL has excellent debug functions making it lightning-fast to see what’s going on in your SQL code. ezSQL can dramatically decrease development time and in most cases will streamline your code and make things run faster.
Features
ezSQL is a widget that makes it ridiculously easy for you to use mySQL, Oracle8, InterBase/FireBird, PostgreSQL, SQLite (PHP), SQLite (C++) or MS-SQL database(s) within your PHP/C++ scripts (more db’s coming soon)
It is one php file that you include at the top of your script. Then, instead of using standard php database functions listed in the php manual, you use a much smaller (and easier) set of ezSQL functions.
It automatically caches query results and allows you to use easy to understand functions to manipulate and extract them without causing extra server overhead
It has excellent debug functions making it lightning-fast to see what’s going on in your SQL code
Most ezSQL functions can return results as Objects, Associative Arrays, or Numerical Arrays
It can dramatically decrease development time and in most cases will streamline your code and make things run faster as well as making it very easy to debug and optimise your database queries.
Works with Smarty templating language
It is a small class and will not add very much overhead to your website.
Download ezSQL completely Free from here , also check out EZ Results Paging Class ezSQL’s sister class that makes result paging a snip.
No comments




