Site optimization

As I wrote before, was turned off by my web host because of too much load on the server. Here is some things I’ve done to fix this. It can be a good guide for others to use in any application for more optimized PHP code.

1. Reduce # of SQL queries

Use as few SQL queries as possible. If you can use one query instead of two, do it. By using the JOIN command in Mysql, you can combine your SELECT results from different tables and reduce your # queries.

When I implemented the site some years ago, I didn’t think about optimization at all. For example, to get 10 usernames from the database I did

SELECT id FROM users LIMIT 0,10

…to get the ID’s of the users (every table I have is populated with an “id” column). After this, I did

SELECT * FROM users WHERE id=1
SELECT * FROM users WHERE id=2
SELECT * FROM users WHERE id=3

…and so forth for all 10 users to get their username. Why? Because the interface in PHP became so nice looking. It made things fast to implement.

The above queries selects all the information from the users (email, encrypted password, information etc) just to get their username. It also does 11 queries for this, which slows things down.

Anyway, I’ve replaced such code with their smarter equivalent, in this case,

SELECT username FROM users LIMIT 0,10

This is just one query and it selects nothing but the information asked for.

2. Compress images

I made a quick fix when it comes to images, too. In the newsfeed there is small images called thumbnails. The thing is… They were not thumbnails, they were the original images. Stupid but quick fix that doesn’t matter if you have just a few number of visitors that all have good internet connection.

Using the original images everywhere is stupid, it uses more than 10 times of what you really need. I implemented a module in PHP for compressing images and handling thumbnails to solve this problem.

3. Limit the use of PHP’s image functions

These image functions in PHP are heavy for the server. By limiting the use of them, my server went from really slow to really fast. At first, I turned them all off to be sure that they were a performance culprit. Then I implemented functions for limiting the use of these. contains many images generated by these functions. To generate one of these images, a call to a script is done. Let’s call it generate.php. This function was called on demand in my old version. Anyone could run the script 10 times in a second and crash the server. I didn’t think of this of course. In my new version, I first check if the target image file already is generated. If it is, generate.php will exit without doing the hard work. This ensures the server load to some extent, but I want to be sure. Therefore, the time of the last image generating is saved i a database. When generate.php starts, it checks if this value is more recent than 5 seconds. If it is, it exits. Now the script is abuse-free and will not generate images too often. Awesome.

4. AJAX and other client-side applications

Instead of clicking a link and updating the HTML page, the user can (in many cases) do equivalent things using JavaScript, AJAX and other client-side applications (like Flash). This saves A LOT of bandwidth.

5. Compress files that are loaded often

Did you know that you can compress JavaScript and CSS files? Well, you can. Do it as often as you can, because they are loaded very often. Below are two random compressors found by Google.

Compress CSS here:

Compress JavaScript here:

“This site has been suspended” (december 2010)



The site above is a temporary server that I have in my livingroom. Some photos are broken but all the entries and patterns are there.

That’s the text popping up when trying to reach I will with this entry try to explain why.

I jumped out of bed late yesterday night when getting an email to my phone saying that my site “Your site causes too much load on the servers, we will shut it down temporarily.”. Well, THANK YOU VERY MUCH FOR LETTING ME KNOW BEFORE YOU SHUT IT DOWN! *not*

The reason why my site is causing more load than other sites are the Generators. The old-fashion-style generator 1 and the alpha generator is on-demand generation of images directly on the server. Now, generating/editing/opening images on a server cause much load because

  • They need more processor power than just sending an image to a user
  • They take up much more space in the RAM than ordinary images

I’ve taken action for these reasons, but it doesn’t seem to be enough.


I’ve sent an email to my web hotel, begging them to start up the site again and that I will temporarily turn off the culprit apps (the generators) and try to replace them with client-side apps like Flash or similar.
The decision to start up the site again lies in the hands of my web hotel in Sweden, and they will hopefully start it up again in any minute. The working hours in Sweden is 08-17 and we are in the timezone GMT+01. The clock is just after eight when writing this.

I hope this answers some questions for now.


Sick of being sick!

If you’ve read the section header you might have guessed I’m sick again. Yay. While not being able to sleep, I tried some random games in the Ubuntu repos. Among them were Wormux, Pydance and Urban Terror.

Wormux, a Worms rip off for Linux, sucked big time. I have nothing more to say.

Pydance is a StepMania-similar dance simulator which is surprisingly adaptive to different kinds of input (dance pads, bongo drums etc), but it also sucked when trying to play it. Are the arrows supposed to lag? Do I have the wrong latency settings on my input? I tried to solve those things, but I left the game without any answer to any of the questions.

Urban Terror. What do you think of when hearing it? Terrorists, police, bombs? Yeah, that’s about it. This is a plain CounterStrike rip-off. I joined some online server games anyway, and I must say that it’s not bad. It may not be CounterStrike, but it’s for sure a good try. In addition to the basic moves in CS, you can do powerslides and wall jumps, which makes things a bit more interesting.

To do: Uninstall all the new games but Urban Terror.

Got a mail from Marina today also. It’s always fun to read her emails. They might be a bit long but they are interesting and mostly positive. Marina is an old friend of mine in the world of friendship bracelets. We’ve helped each other for a long time over the net with different things, mostly things related to copyright of patterns.

Until next time.