utorak, 27. ožujka 2012.

MySQL Stored Procedure - application example

USER_ACTIVITY

user_id nightmorningdayeveninglast_update
1 4.62.43.68.62012-03-26 16:22:42
214.29.98.210.42012-03-26 07:45:22
31.26.36.513.12012-03-25 22:12:55
43.12.65.22.92012-03-24 11:34:12
...

distribution of hours:
night = 21h-5h
morning = 5h - 10h
day = 10h - 16h
evening = 16h-21h


-------------------------------------------------------------------------------------------------

Say you need to make record of users' activity. Each time you check upon a user and see that he was active since you last checked him, you add 1 point to his activity.

Example:
Current time is: 2012-03-26 20:42:38
We use some other service (not important which one) to find out whether user was active since we last checked him or not.
For simplicity, we assume that all users had some activity since the last time they were checked.

I check upon user#1.
I need to distribute 1 point of activity across the times of day.
Since it was the same evening since we last updated this user, we add 1 point to 'evening activity'.

Then i check upon user#2.
Since his last update 12.95 hours passed. 2.25 hours of that time was in the morning, 6 hours were during the day and 4.7 hours was during the evening. In the same ratio, 1 point should be distributed to the day parts (morning, day, evening). This means: 0.174 to 'morning', 0.463 to 'day' and 0.363 to 'evening'.

In the case of user#4 we would like to do something like this:
Since last update of this user 57,133 hours passed. We create variable hour_inc = 1/57.133.
Then we add (hour_inc*8) to 'night', (hour_inc*5) to 'morning', (hour_inc*6) to 'day' and (hour_inc*5) to 'evening'.
Sumation of these increments in this case is less than 1.

So if we want to do this for arbitrary 100 users from this table and we want to update existing information based on the information we just calculated, how would we do it?
Is it possible to do it with one with one SQL statement?
I don't think so... if you know let me know...

So the first thing we do. We open our programming language of choice and make a function that will do this for us:
{
    1) do a SELECT query to fetch needed data
    2) iterate through users and calculate new activities for each one
    3) after that, we do a 100 of consecutive update statements. Each update statement is different because 'last_update' time for each user is different
}

The other approach is using stored procedures.
We make a procedure that uses a cursor and iterates through all users.
For each of them builds up a big UPDATE statement (string) using concatenation in combination with some form of loop.
After a statement is built, we use PREPARE-EXECUTE to preform a statement which was up to now in a some variable of type VARCHAR(1500).

The thing that I observed is that using this approach 20000 users gets correctly updated in up to 7 seconds.
Then I modified the stored procedure so that it doesn't apply those update statements but instead prints them out using SELECT command.
I copied one of update statements, made a complement update statement that annuls first one's effect.
Then I copied this pair many times until I had 500 update queries. I executed them and waited 34 seconds before it finished.
That is the duration of merely updating information in database, not including the time it took to calculate this data that is being put in database. While the stored procedure does the calculation + update within 7 seconds for 20000 users.

If I did everything right, THAT is pretty impressive.

utorak, 20. ožujka 2012.

Erasmus student exchange at Porto

It's been 3 weeks since I arrived to Porto. I'm a student of computer science from Croatia, now studying at Faculdade de Engenharia da Universidade do Porto - FEUP.

I got here by myself so I basically didn't know anyone the first few days. In some way I feel like I still don't know anyone, but I guess that depends on how do you define "knowing someone".

So, what are the experiences so far...

Portuguese girls in average are not as nearly as pretty as Croatian girls.



They put kitchen tiles on the outside of their houses in the old city core.



There is a tradition in Portugal that the students wear funny looking "Harry Potter" outfits (black suit with some kind of cape).
It is not obligatory, but they respect this tradition very much. 
They group in some way, and leaders of those groups also carry around some sort of wooden spoon that I first thought was a rowing paddle. They wear it all the time when they are outside, regardless of the weather. It amazes me to see how many of them are doing it. It's like carnival whole year long.
Not everybody can wear it, though... First year students need to pass the initiation period during which they are compelled to do all sorts of humiliating activities. This lasts from the start of academic year until May.
In May there is a holiday with huge ceremony in the streets of Porto where all freshmen can finally wear these black uniforms.

In a way I can understand it because there is a lot of young student that come here from many parts of Portugal to an unknown environment to study. For them it's a way to bond to other students who are in similar situation.
Still, I could never picture myself wearing that.




A majority of people don't speak English. I guess that is the case in all nations that speak one of the biggest languages in the world - Chinese, Spanish, Portuguese, French, German, Italian, ...

People are often late for appointments. And I don't mean 5-10 minutes late. 30 minutes to 1 hour and 30 minutes... that's the range. Or many times they just don't show up. This is not just my observation, I heard it from a lot of people.

The Erasmus students here are 50% Brazilians... Prior to my arrival, I heard all these stories about Brazilian hedonistic life-style and general laziness. But I have had contact with only one Brazilian with those characteristics and only over e-mail.

In my experience, foreign students come here to party. It's high school all over again for them. Party all the time, drinking also... I don't mind that. I like beer, sangria, martini... But the way they drink, it's not for humans.

Before I came here, my expectations were not high. I thought I will make some Portuguese friends, go surfing, play futsal in the afternoon, learn some basics of Portuguese language... basically integrate myself in a domestic culture.
These goals now seems hardly achievable. People here have their own lives, friends, daily routines, families... it's difficult to get involved in all of that.
On the other hand, the Erasmus students are more often willing to connect with new people. So that makes foreigners form their own groups instead of mixing with the locals.

I live in student residence Alberto Amaral. It's cheap (130€), I have two-bed room all for myself. But at any time that could change. I don't have kitchen, but student canteen is a hundred meters from the dorm. I have my own fridge. There is a microwave in the lobby, also a big fridge and TV with like a hundred channels. I don't it watch very often, only English Premier League on weekends and Champions League during some weeks.
One thing to take into consideration if you are on exchange in Porto during winter or early spring: ordinary flats here don't have any kind heating installed. Rooms in a dorm do.

Laundry is done in a special room here in a dorm. You need your own detergent, softener and whatever... Washing machine works on special coins. One coin costs €2. There is also a dryer (usage is free, no coins), and the ironing board.

Student canteen, price of lunch or dinner is €2.30, but it's not even worth the money. You always have 4 menus at your choice: Vegetarian, Meat, Diet, Fish. I only haven't tried Diet. Of other three, Vegetarian seems to be the best choice every time. The meat in the canteen is simply awful.

Transportation.
The city is divided in zones. When you buy a monthly ticket, you have to choose which zones you want to include. Price of monthly ticket depends on number of zones you use. I use two zones and pay monthly ticket €22.5.

For now my estimation of monthly expenses are like this:
€130 - accommodation
€30 - transportation
€280 - food
€7.5 - phone

Those are some kind of fixed monthly costs. There are also one-time purchases (glasses, spoons, knives, cleaning detergent, sponges, iron...) and variable monthly costs. Variable monthly costs (drinking, chocolate, ordering a pizza, ...) you can always cut down if you need to save up money.