Category: SQL

  • mysql copy table

    Continuing my chain of blatantly obvious mysql queries that I nonetheless didn’t know at one stage 🙂 copying a table isn’t an obvious function in mySQL. So here’s how to do a mysql copy table

    Say you have a table called “tbl_theoriginal” which has the perfect, ideal, wonderful table structure for a new table you want “tbl_thenewone” and you would like to create it the query you would create is as follows:

    CREATE tbl_thenewone SELECT * FROM tbl_the original

    Simple huh! You can put in any WHERE clause you like and even a limit 1,1 or whatever if you don’t want to transfer any content but just the table structure.

    Get my marketing cheat sheets at Click Here
  • mySQL rename table

    One of the (few) downsides of phpMyAdmin (at least the version I use) is that it doesn’t support the mySQL rename table function.

    It is very simple to use this function through the SQL tab in phpMyAdmin (or any other direct interface with mysql). Say you want to change the name of the table from “tbl_oldtable” to “tbl_newtable” then this would be the code:

    RENAME TABLE tbl_oldtable TO tbl_newtable

    Simple huh! Right with that duty to my readers and hopefully my natural search traffic… (these mySQL posts tend to do pretty well) I am off to drink more cabernet sauvignon 🙂

    Get my marketing cheat sheets at Click Here
  • Code Performance is Important

    The puzzles that a fair few geeky companies post on their jobs pages to try and vet/encourage great coders to complete them and apply for jobs are quite cool (I definitely don’t fit in the great coder bucket). Recently I noticed a fair few of them are focussed on code performance and so thought I would share my experience of code performance harming my site.

    The graph above is the exit rate for users visiting my display cocktail pages on my cocktail site (according to Google analytics which has pretty graphics but some data issues) against time. The initial decrease in exit rate is awesome and primarily due to implementing the cocktail recommendation engine. Suddenly in mid July there is a massive jump in exits, this coincided with a process I was running for my facebook app really screwing up and slowing down every query run on my database :(. I shut that query down (although didn’t realize what was happening for a month) and (for the moment) have given up on my facebook app to concentrate on the main site. After shutting down the process performance and exit rates improved instantly.

    I don’t monitor the performance of the scripts on my site anywhere near enough. Last weekend I built tracking to monitor the ranking of cocktails on my site weekly, pull out fast moving cocktails and so on. I have started logging keyword searches on my site too as of last week. This weekend a key focus for me is producing a performance dashboard summarized by hour of day and day of year. I am going to start monitoring the efficiency of all changes I make and see where I need to make improvements.

    Cocktailmaking.co.uk has grown 3x since last year. The site also gets 25% of it’s visits in December with 10% of those on New Years’ Eve. I need to make the site much more efficient before that date comes or my servers will be a smoking mess this year and I’ll capture none of the revenue that I should.

    Code efficiency is really important.

    Get my marketing cheat sheets at Click Here
  • Cocktail Recommendation: Part 4 – the results

    Wow did it work or what! The cocktail recommendation engine I produced earlier this year has been a fabulous success. I set up an AB test of the cocktail recommendation by randomly assigning my visitors to either see the cocktail recommendation engine or not and the main target was to reduce the bounce rates of visitors to my cocktail recipe pages (both buckets contain c. 1million page views).

    The above graph shows you the overall impact of the test across all my cocktails with 100days of data. This impact is diluted because of the volume of cocktails for which I don’t have enough data to produce cocktail recommendations so below is a list broken out by cocktail id (I have removed the names since work people see this blog and some are naughty). The cocktails in the list below are essentially the cocktails found as the first 10 here (you are warned… that page contains rude words) under the tab “Graph of cocktail recipes in order”.

    On average for the top 10 cocktails the recommendation engine reduced the bounce rate by 16% with some cases (esp. slippery nipple) having an impact as high as a 31% reduction in bounce rate for the page.

    Get my marketing cheat sheets at Click Here
  • Understanding Cocktail Rating Distribution

    Something interesting with my facebook application is that in the past week I have gathered >1000 ratings on the cocktails in my database from 745 unique users of my cocktail making application. This is a fairly small number for data analysis but 3 things stood out:

    The most votes were given in the bucket of “10” – people seem to tend to vote on the cocktails they like.

    The cocktails that were rated 10 tended to get more votes in general i.e. 3 votes per cocktail and not the average 1.5

    When looking just at the users who gave a ten and what % of each voting bucket they made up it became clear that these users were pretty much all or nothing guys relative to the crowd, either they gave a cocktail a 10 or they gave it nothing.

    More to come on user voting behaviour over time I am sure and also on the most popular cocktails, cocktail ingredients, favourite cocktails lists and so on… I now have far too much data to work through and that is great!

    Get my marketing cheat sheets at Click Here
  • Cocktail Recommentation Engine: Part 3 – The First Results

    The following table is a subset of the output results from the cocktail recommendation engine (be warned some rude words in some of the cocktail names). I have tried to drill the list down to just the best matches so bear in mind there are probably 30% more matches thrown out which appear to have statistical significance but I can find no reason for the relationship (beyond sometimes they are next to each other in the large cocktail A-Z list).

    Cocktail One – the cocktail someone is on, Cocktail Two – the cocktail I recommend, Probability – the probability that if someone is on cocktail one and visits another cocktail two will be that cocktail. There had to be at least 10 relationships in total recorded between cocktail one and another cocktail in my db for a cocktail to make this table.

    I am hoping that as I get more data this table will grow more accurate and I will have fewer “wtf?” moments looking at the list. I also want to add two more probabilities to the calculation:

    1. Probability if they visit 1 they will like 2
    2. Probability if I display 2 on 1 they will click on it

    Anyway for now enjoy looking at the cocktail relationships and expect more data soon.

    Get my marketing cheat sheets at Click Here
  • Huge steaming piles of data (Project Cocktail Part 2)

    Today has been quite challenging in project cocktail. The main issue is working out how much information to store and what granularity to summarize it on. Data is pouring in rapidly, I have defined over 5000 directional cocktail relationships and probably 10% of my cocktails within one day have at least one relationship I would define as relevant (I shall work on statistical significance fairly heavily later on).

    The issue is that every row in my database is currently taking up 46bytes and I am adding c. 7.5k rows a day. The index on the database is then adding another 25% to this and there is little I can do here since I am required to create a primary key. Therefore daily I am creating 0.4MB of data in unsummarized form. The machine I am using has 100MB of storage and so I could store up to 250 days of raw (unsummarized) data or 2.17million rows (handy cos right now my cocktail DB could theoretically create 2.17million different directional relationships with different likelihood factors).

    In the short term I am going to do nothing more than summarize on a month basis and depreciate the value of prior months over time but in the long term I want to know if recommendations should differ significantly by various user related variables and hence I want to store those variables so that in the future I can have a significant dataset to query and work out the impact of those variables. My target is to make this engine slightly scary at predicting what cocktail you might want to see next and to hit the scary threshold will take a little more than the #1 most likely relationship to deliver. So all things considered I have 0.5TB of data storage going spare right now in 5 mySQL databases… let’s go for the big data 🙂 and see what comes out!!!

    Tomorrow I will be helping move 100 rowing boats so that my new boatclub can undergo an awesome renovation over the next 3wks which means I will have to take a break (and let the data gather into an even higher volume). Hopefully Sunday I will be able to do some analysis and maybe even build a mock up that shows what the output could start to look like. For now goodnight!

    Get my marketing cheat sheets at Click Here
  • Recommendation Engine: Project Cocktail Part 1

    Ok so how does one build a recommendation engine?

    For the last year I have been tweaking improving and revising my cocktail recipes website with the prime goal of driving more traffic to the site. I have certainly acheived some of that with >1500 visitors daily on most days at the moment (still not huge but really massive growth). What is noticeable though compared to my paper airplanes site is a distinct lack of loyalty in my user base. With paperairplanes.co.uk ~32% of the visits daily are from returning users but with cocktailmaking.co.uk only ~9% are.

    I need to make the site better and so my first attempt is to produce a cocktail recommendation engine. Phase one of the project just released to the site and I wanted to chat about that a little bit here.

    I am logging every chain of two cocktails viewed by a visitor to my site within 2hrs. My thinking is that time spent and number of clicks browsing both degrade the correlation between cocktails in a user’s browse path and so two cocktails viewed back to back in a short time period are likely to be very closely related. If I observe this interaction over millions of such pairs (data I can gather in just a few months) the most correlated cocktail combos should stand out from the mix at least for the top 10% of cocktails viewed on my site. That is my thinking and I will start to share the results on here as the data gathering proceeds.

    The wonders of having a website with enough traffic to make analysis interesting 🙂

    Get my marketing cheat sheets at Click Here
  • SELECT … CASE expressions rock!!!

    CASE expressions are a huge timesaving SQL feature that I have just recently fallen in love with. The specific example was trying to find how the activity of a class of users has declined over a period of twelve months. Without CASE this would have required writing and executing 12 different queries and then joining them into a table either by joining all those queries together or by pasting them into (in this case) excel. That’s hard enough but what about 24hrs in a day for day parting analysis across 100000 keywords (far too big for excel <2007 that one ;) )

    CASE selects each of those different queries I described into a column using a clause something like:

    SELECT sum( CASE month WHEN 2006M10 THEN revenue ELSE 0 END) as oct06, sum( CASE month WHEN 2006M11 THEN revenue ELSE 0 END) as nov06, sum( CASE month WHEN 2006M12 THEN revenue ELSE 0 END) as dec06 FROM tbl_userrevenue WHERE userid IN (‘alex’, ‘beth’)

    Which in my mind is a pretty cool tool for analyzing anything where you want to look at discrete time intervals or users classes or almost anything else individually.

    Get my marketing cheat sheets at Click Here
  • PHP mySQL close connection

    So I have been creating lots of new features for me to maintain my cocktail making website. One feature has involved me setting up a new mySQL 5.0 database (with cool stored procedures I am only starting to understand now) in order to do some tracking of what is happening on the site and with my widget. This has required me to use two databases at once for the first time. One to check if a user is signed in with admin rights and the other to check the tracking out of the other database (php security is really tricky so I am slightly nervous posting even this here :s ).

    However I did want to note how you close a connection with a mysql database using PHP in order to open one with another mysql database since it’s really simple but I found it hard to get info. So when you open databases try closing them, I know I will in future for good practice, it makes everything much simpler if you add another database at a later stage.

    Get my marketing cheat sheets at Click Here