Archive for category database

speeding up your mysql db

This is an interesting video on how to speed up your mysql db. Be sure you have an hour of free time if you want to watch this.
If you want to look for more videos on how to to tech stuff I recommend you take a look at bestechvideos

The Quick and Easy Guide to Speed Up MySQL for Web Developers from Jonathan Levin on Vimeo.

Tags: , , ,

Useful Propel criteria methods and constants

I’m working with criteria quite often lately and must say it is a handy way of query writing. The only problem I have with criteria is that I don’t seem to find a simple overview (list) of the most important methods you can add to it. I don’t really like the Propel website as I don’t find the thing I need in a few seconds and that is a must for a lot of people. If I create a list of the most important criteria methods for myself, I rather share it with you guys…

Simple Select query with 2 criteria to check:

$c = new Criteria();
$c->add(AuthorPeer::FIRST_NAME, "Karl");
$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$authors = AuthorPeer::doSelect($c);
// $authors contains array of Author objects

In SQL this will be:

SELECT ... FROM author WHERE author.FIRST_NAME = 'Karl' AND author.LAST_NAME <> 'Marx';

It’s quite simple to write the criteria, the only thing needed to write them is a list of options.

Read the rest of this entry »

Tags: , , ,

forgotten syntaxes for MySQL

A lot of people seem to have problems with updating or inserting data in tables in an intelligent way. We shouldn’t point a finger to those who don’t use the correct syntax for the problem it solves, because there are a lot of different ways to do this, but I wanted to create a little list of interesting syntax’s that are often forgotten and never used. I will talk about:

  • insert… on duplicate key update
  • replace
  • insert… select
  • Load data infile and select into outfile
  • delayed

Read the rest of this entry »

Tags: , , , , , , , , , ,

To reinforce our IT team, we are hiring a colleague (M/F)

As we are growing we are in need for a new junior developer to create and maintain websites. If you or someone you know is interested in this job feel free to contact us. The company is Step-@-Site and we are located in Brussels (Vorst/Forest). Our portfolio contains websites/newsletters/intranet sites for small and big companies, for example;

Your profile:

- Bachelor studies in ICT (junior)
- PHP, MySQL, ASP, JavaScript, HTML and CSS
- Thinking in terms of performance, response time and usability of interfaces
- Systematic and dedicated personality

Offer:

We offer an interesting salary and fringe benefits, linked to your personal competencies and your experience.

Feel free to contact me on kennethvr [AT] devexp [DOT] eu

Tags: , , , ,

Storing multiple data in 1 database field with PHP serialize / unserialize

While the serialize/unserialize functions exists since the early PHP 4 version, I ignored its existence. I discovered it recently doing some research on how to store multiple data in 1 database field.

Before continuing, keep in mind that it’s not always recommended to store multiple data in a database field, because it will be very difficult, if not impossible, to create queries using the inserted data. All serialized data will have to be unserialized, which takes time.

But why did I use it ?

The website I work on has a widget system (like netvibes and igoogle). Each widget has default settings that can be altered by a user. Those changes need to be saved in a database, so the next time the user loads the widget it will make use of the new settings.

When I first had to create this, I did not know of serialization in php, so I created the following db structure. (trying to respect DB Normalization).

widget and settings

widget and settings

You can see that I have a table with a list of widgets and a list of possible settings. Those two tables are connected to a widgetSetting table where the value of a setting to a widget is stored. It worked perfectly but it is not so proficient :

* To retrieve a widget’s settings, you need to join 2 tables.
* To insert new widget settings, you need to check the settings table if a setting exists or not.
* When updating widget settings, you need to check if a setting is still used or not.

This is a lot of work and database complexity for something that only is used for saving/retrieving settings of a widget. In my case it would be much better if I could save those settings in 1 field in the widget table.

widget

widget

This is how I serialize my settings :

$settings = array(
'itemsOnPage' => 30,
'showChart' => true,
'startDate' => '01-10-2008',
'endDate' => '31-10-2008'
);

$serializedSettings = serialize($settings);

/**
* outputs:
*
* a:4:{s:11:"itemsOnPage";i:30;s:9:"showChart";b:1;s:9:"startDate";s:10:"01-10-2008";s:7:"endDate";s:10:"31-10-2008";}
*
*/

The serialize function will generate a storable representation of my array.
Just save this string in a database field and the job is done. To rebuild the original array from this value you only need to call the unserialize function:

 $storedRepresentation = 'a:4:{s:11:"itemsOnPage";i:30;s:9:"showChart";b:1;s:9:"startDate";s:10:"01-10-2008";s:7:"endDate";s:10:"31-10-2008";}';
$settings = unserialize($storedRepresentation);

/*
* print_r($settings) would give :
*
* Array
* (
*  [itemsOnPage] => 30
*  [showChart] => 1
*  [startDate] => 01-10-2008
*  [endDate] => 31-10-2008
* )
*/

As you can see the unserialize function correctly rebuild the original array.

It is also possible to serialize Objects with the exception of the built-in objects.

class MySettings {

private
$itemsOnPage = 30,
$showCart = true,
$startDate = '01-10-2008',
$endDate = '31-10-2008';

public function getItemsOnPage() {
return $this->itemsOnPage;
}
}

$mySettingsSerialized = serialize(new MySettings());

$mySettings = unserialize($mySettingsSerialized);

echo $mySettings->getItemsOnPage(); // 30

When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize() the __wakeup() member function is called.

class MySettings {

private
$itemsOnPage = 30,
$showCart = true,
$startDate = '01-10-2008',
$endDate = '31-10-2008';

public function getItemsOnPage() {
return $this->itemsOnPage;
}

public function __wakeup() {
$this->itemsOnPage *= 10;
}
}

$mySettingsSerialized = serialize(new MySettings());

$mySettings = unserialize($mySettingsSerialized);

echo $mySettings->getItemsOnPage(); // 300

So you see it is very simple to use the seriailize/unserialize functions in php. But like I said in the beginning of the post, use it wisely when storing the serialized value in a database. I only use it when I do not need the value for creating sql queries.

Tags: , ,