When writing an application in PHP, I like to use Justin Vincents ezSQL for database access. ezSQL allows you to write your query and get your results as a PHP object, as shown in the following example:
// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
So one common task you might want to do is to present the retrieved result as a table. While the ezSQL library features a debug() method, this should obviously only be used during development. So if you want to display your result as a table, you’ve got to do it yourself and I hereby provide a snippet of code on how to do it…
So after you’ve fetched your result from the database you can use the following method to print a HTML table with your results:
Read the rest of this entry
Today a colleague approached me and asked about the difference between the following two statements, because they return different results:
select count(*) from mytable;
select count(name) from mytable;
Read the rest of this entry
During maintenance, I had to disable all constraints of a table. I knew that Oracle SQL Developer (I really like it even though it is a developer tool and not aimed at Database Administrators) had built-in functions to do this, but since I could only access the database machine via SSH, I had to do it in SQL*Plus.
Luckily, SQL Developer is quite transparent about the commands it uses and I could therefore easily see what is going on when you disable all constraints on a table using the GUI. So here it goes…
Read the rest of this entry
On one database, I noticed that SQL*Plus did not show the heading of the columns when I ran a query. The result was this:
SQL> select instance_name,status from v$instance;
mydb OPEN
Even when entering “set heading on” didn’t change anything. I then investigated and found out that someone had changed the $ORACLE_HOME/sqlplus/admin/glogin.sql
file (SQL*Plus runs this file on startup) and added the following lines:
SET LINESIZE 150
SET PAGESIZE 0
While this is quite nice and replaced the suboptimal default values, this was the cause for my problems mentioned above. Changing the line “SET PAGESIZE 0” to “SET PAGESIZE 1000” solved the problem and now, the query shows up the way I wanted:
SQL> select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
mydb OPEN
For habere.ch, we launched a contest where we award a restaurant voucher to a random person that wrote a review of a restaurant on our site. For this, we will be using the following query:
SELECT DISTINCT comment_author,comment_author_email
FROM wp_comments
ORDER BY RAND()
LIMIT 1;
What this query does is, that it selects all distinct rows, orders them randomly and shows the first entry of this selection.
Note that this query is relatively slow and should not be used in a program. When you use EXPLAIN
on this query, MySQL will explain to you that it will create a temporary table and run a sort. Then only one entry is returned. But when you just use it once to determine a winner, this will work just fine!
Today I had to import some data from a CSV file into a table on a MySQL server.
So here is how to do it:
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
Source
Of course, you will need to have access to the machine where the database is running. As an alternative, I am sure there are developer tools that can enter the data remotely.
One of the regularly recurring tasks is to apply a Patchset to a database. This post contains the basic steps required to apply Patchset 10.2.0.5 to an unpatched Oracle 10g Database (10.2.0.1). I am using this post mainly for my own reference, so please do not expect an extensive step-by-step guide. Note that you need Oracle Support (formerly Metalink) to access and download the patches.
Before we begin, I must state that I provide these instructions based on my experiences with database upgrades and these are by no means complete. When applying a Patchset, always refer to the patch_note.html provided in the installation media. This means you can use my steps only as a coarse guide. These steps can also be used to create your own “Upgrade Checklist”.
Read the rest of this entry
This week and next week I am participating in an Oracle Architecture and Internals training at Trivadis in Berne. Today, we received an overview of Oracle (Editions, Licensing, Support) and took a first look into the Oracle Architecture. This first chunk of information included Parameters, Memory and Process Structures. While looking at the PGA structures, I noticed a graph showing “PGA Cache Hit Percentage” in relation to the sizing of the PGA.
The term “PGA Cache Hit Percentage” does not make sense, since there is no cache in the PGA. According to my training material, the PGA consists of the following structures:
Read the rest of this entry
Today I wondered how my newly purchased Virtual Server performs under load. I am currently using lighttpd as a webserver and MySQL as the database for WordPress. I already knew that MySQL features the Query Cache, which stores the result of issued queries in memory (Oracle’s equivalent of this would be the Result Cache in Oracle 11g). I wondered how much this feature could improve performance on a normal WordPress blog.
I quickly wrote a small Java program to query my website (source code available here: TestWebPerfo.java) and retrieve the performance metrics from the HTML source code. I then ran it 500 times to get my metrics without the Query Cache turned on. Average time for building the website: 0.115 seconds.
Read the rest of this entry
Having security in mind, I had some concerns granting all privileges to the WordPress MySQL user (see the instructions from WordPress):
GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername" [..]
After all, with these privileges the WordPress user would be able to access other databases on this server and do whatever he likes. WordPress has become very popular and is a known target for exploits and the like (as a quick search on milw0rm.com will confirm). I didn’t like that idea.
So here is what I did:
GRANT SELECT, INSERT, UPDATE, CREATE, DELETE ON wordpress.* TO 'wordpress' IDENTIFIED BY 'mypass';
This works fine so far and I don’t think my WordPress installation needs more privileges. Note that the ALTER and DROP statements are missing from my list, which could interfere with future updates. But we’ll see…