{
  "id": 2024,
  "title": "techniques to produce a result set",
  "url": "/competitions/FacebookRecruiting/discussion/2024",
  "author_name": "",
  "post_date": "2012-06-10T19:23:13.467Z",
  "votes": null,
  "comment_count": 12,
  "views": 6590,
  "content": "<p>Ok, so I loaded the data into a mysql database, ran a simple select statement i.e. select * where source_node = 24;</p>\r\n<p>it takes roughly 1 min to run!!!! If I have to do this for &#43;- 200000 records it'll take 300 days.</p>\r\n<p>Are there ways to speed up the db querying (first thing in my mind was to put the database on a db hosting site but I'm not sure that'll make it any quicker)</p>",
  "messages": [
    {
      "id": "11511",
      "postDate": "06/10/2012 19:23:13",
      "content": "<p>Ok, so I loaded the data into a mysql database, ran a simple select statement i.e. select * where source_node = 24;</p>\r\n<p>it takes roughly 1 min to run!!!! If I have to do this for &#43;- 200000 records it'll take 300 days.</p>\r\n<p>Are there ways to speed up the db querying (first thing in my mind was to put the database on a db hosting site but I'm not sure that'll make it any quicker)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11512",
      "postDate": "06/10/2012 19:40:38",
      "content": "<p>add index on both columns?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11513",
      "postDate": "06/10/2012 20:08:03",
      "content": "<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11514",
      "postDate": "06/10/2012 21:39:45",
      "content": "<p>[quote=osphere;11513]</p>\r\n<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>\r\n<p>[/quote]</p>\r\n<p>Do you think that Facebook does not use databases to &quot;reduce the overhead&quot; as you put it?</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11515",
      "postDate": "06/10/2012 22:46:51",
      "content": "<p>There are noSQL databases to deal with huge information. And about that time for executing a queue.. maybe you need to put an index on the node columns.\r\n<br>\r\nBut i dont think that we need an db for a dataset this size...</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11517",
      "postDate": "06/11/2012 06:49:20",
      "content": "<p>[quote=Momchil Georgiev;11514]</p>\r\n<p>[quote=osphere;11513]</p>\r\n<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>\r\n<p>[/quote]</p>\r\n<p>Do you think that Facebook does not use databases to &quot;reduce the overhead&quot; as you put it?</p>\r\n<p>[/quote]</p>\r\n<p>&nbsp;</p>\r\n<p>They do use databases in facebook, but remember: you gotta use the right tool for the job</p>\r\n<p>MySQL or any other relational database has a lot of overhead to maintain ACID, which may be right for some jobs, but not this one.</p>\r\n<p>noSQL databases as Leustagos said is like.. having all your dataset in RAM which is similar to writing your own program where you can manage memory management and access. Which may be helpful for this job, but again: having your data inside a database has\r\n a some overhead (maybe the overhead can be neglected on a noSQLdatabase, I have no idea, however it exists, thats for sure)</p>\r\n<p>I suggest to djmachet to try using a different solution than MySQL, thats all</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11523",
      "postDate": "06/11/2012 15:59:41",
      "content": "<p>Create indexes on both columns.</p>\r\n<p>I put it on a mysql table and the same query takes 118ms on my laptop (2.5GHz intel mac)</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11528",
      "postDate": "06/11/2012 18:03:06",
      "content": "<p>I generate an adjacency matrix eta with<br>\r\neta(i,j)=1 if there is an edge from i to j, 0 otherwise<br>\r\nI know that this method is the worst in terms of memory consumption, but it is the fastest to look up whether there is an edge or not.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11533",
      "postDate": "06/11/2012 19:34:45",
      "content": "<p>You could generate an adjacency list using hashmaps and hashsets... space would be a little worst than pure list of adjacencies but lookup will be O(1).\r\n<br>\r\nI'm using it.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11534",
      "postDate": "06/11/2012 21:28:34",
      "content": "<p>I created myself some little helpers. I have two hash maps (dictionaries in python). The first has a node as a key and list of vertices to which it points as its values. The second has a node as a key and a list of vertices which point to it. The latter\r\n is useful if interested in following the edges backwards quickly. Looking up a node is pretty much instantly on my laptop.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11536",
      "postDate": "06/11/2012 22:44:08",
      "content": "<p>Don't use a db for this. A python dictionary is more than adequate. However, if you do start to create attributes for a node, then maybe a db would be handy, but use sqlite with loads of indexes.<br>\r\nStill, Christian has the right idea. Spend a bit of time creating dictionaries (of A to B and B to A) upfront and then pickle them to the hard drive. That way you can get access to them quickly.\r\n</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11539",
      "postDate": "06/12/2012 00:49:33",
      "content": "<p>[quote=Leustagos;11533]</p>\r\n<p>You could generate an adjacency list using hashmaps and hashsets... space would be a little worst than pure list of adjacencies but lookup will be O(1).\r\n<br>\r\nI'm using it.</p>\r\n<p>[/quote]</p>\r\n<p>As a small variation to this approach, you can get O(1) lookup with a smaller constant than a hash table by replacing the use of the hash table with an array. This lets you directly index into the array using the node index, at the cost of potentially wasting\r\n some memory if many of the indices smaller than max_node_index correspond to nodes that don't exist or have no associated values.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "11685",
      "postDate": "06/17/2012 14:04:47",
      "content": "<p>djmachet, no need for a DB due to its overhead. A 'awk' command is acceptable enough for your query 'select * where source_node = 24;' :D</p>\r\n<p>$ time awk -F, '$1==24{print}' train.csv<br>\r\n24,322832<br>\r\n24,423929<br>\r\n24,1435<br>\r\n24,1310872</p>\r\n<p>real 0m16.000s<br>\r\nuser 0m15.718s<br>\r\nsys 0m0.202s</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 11512,
      "author_name": "cmis91",
      "author_url": "",
      "post_date": "06/10/2012 19:40:38",
      "content": "<p>add index on both columns?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11513,
      "author_name": "osphere",
      "author_url": "",
      "post_date": "06/10/2012 20:08:03",
      "content": "<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11514,
      "author_name": "sirguessalot",
      "author_url": "",
      "post_date": "06/10/2012 21:39:45",
      "content": "<p>[quote=osphere;11513]</p>\r\n<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>\r\n<p>[/quote]</p>\r\n<p>Do you think that Facebook does not use databases to &quot;reduce the overhead&quot; as you put it?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11515,
      "author_name": "leustagos",
      "author_url": "",
      "post_date": "06/10/2012 22:46:51",
      "content": "<p>There are noSQL databases to deal with huge information. And about that time for executing a queue.. maybe you need to put an index on the node columns.\r\n<br>\r\nBut i dont think that we need an db for a dataset this size...</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11517,
      "author_name": "osphere",
      "author_url": "",
      "post_date": "06/11/2012 06:49:20",
      "content": "<p>[quote=Momchil Georgiev;11514]</p>\r\n<p>[quote=osphere;11513]</p>\r\n<p>I truly believe putting it into a DB will kill performance... you can optimize it all you want but I dont think a DB is the solution</p>\r\n<p>You should really try to write a program where you can manually manage memory as it will reduce the overhead that a DB has</p>\r\n<p>[/quote]</p>\r\n<p>Do you think that Facebook does not use databases to &quot;reduce the overhead&quot; as you put it?</p>\r\n<p>[/quote]</p>\r\n<p>&nbsp;</p>\r\n<p>They do use databases in facebook, but remember: you gotta use the right tool for the job</p>\r\n<p>MySQL or any other relational database has a lot of overhead to maintain ACID, which may be right for some jobs, but not this one.</p>\r\n<p>noSQL databases as Leustagos said is like.. having all your dataset in RAM which is similar to writing your own program where you can manage memory management and access. Which may be helpful for this job, but again: having your data inside a database has\r\n a some overhead (maybe the overhead can be neglected on a noSQLdatabase, I have no idea, however it exists, thats for sure)</p>\r\n<p>I suggest to djmachet to try using a different solution than MySQL, thats all</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11523,
      "author_name": "esteban1",
      "author_url": "",
      "post_date": "06/11/2012 15:59:41",
      "content": "<p>Create indexes on both columns.</p>\r\n<p>I put it on a mysql table and the same query takes 118ms on my laptop (2.5GHz intel mac)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11528,
      "author_name": "commandercain",
      "author_url": "",
      "post_date": "06/11/2012 18:03:06",
      "content": "<p>I generate an adjacency matrix eta with<br>\r\neta(i,j)=1 if there is an edge from i to j, 0 otherwise<br>\r\nI know that this method is the worst in terms of memory consumption, but it is the fastest to look up whether there is an edge or not.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11533,
      "author_name": "leustagos",
      "author_url": "",
      "post_date": "06/11/2012 19:34:45",
      "content": "<p>You could generate an adjacency list using hashmaps and hashsets... space would be a little worst than pure list of adjacencies but lookup will be O(1).\r\n<br>\r\nI'm using it.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11534,
      "author_name": "tafkas",
      "author_url": "",
      "post_date": "06/11/2012 21:28:34",
      "content": "<p>I created myself some little helpers. I have two hash maps (dictionaries in python). The first has a node as a key and list of vertices to which it points as its values. The second has a node as a key and a list of vertices which point to it. The latter\r\n is useful if interested in following the edges backwards quickly. Looking up a node is pretty much instantly on my laptop.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11536,
      "author_name": "diarmuidwrenne",
      "author_url": "",
      "post_date": "06/11/2012 22:44:08",
      "content": "<p>Don't use a db for this. A python dictionary is more than adequate. However, if you do start to create attributes for a node, then maybe a db would be handy, but use sqlite with loads of indexes.<br>\r\nStill, Christian has the right idea. Spend a bit of time creating dictionaries (of A to B and B to A) upfront and then pickle them to the hard drive. That way you can get access to them quickly.\r\n</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11539,
      "author_name": "roobs5218",
      "author_url": "",
      "post_date": "06/12/2012 00:49:33",
      "content": "<p>[quote=Leustagos;11533]</p>\r\n<p>You could generate an adjacency list using hashmaps and hashsets... space would be a little worst than pure list of adjacencies but lookup will be O(1).\r\n<br>\r\nI'm using it.</p>\r\n<p>[/quote]</p>\r\n<p>As a small variation to this approach, you can get O(1) lookup with a smaller constant than a hash table by replacing the use of the hash table with an array. This lets you directly index into the array using the node index, at the cost of potentially wasting\r\n some memory if many of the indices smaller than max_node_index correspond to nodes that don't exist or have no associated values.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 11685,
      "author_name": "nhanitvn",
      "author_url": "",
      "post_date": "06/17/2012 14:04:47",
      "content": "<p>djmachet, no need for a DB due to its overhead. A 'awk' command is acceptable enough for your query 'select * where source_node = 24;' :D</p>\r\n<p>$ time awk -F, '$1==24{print}' train.csv<br>\r\n24,322832<br>\r\n24,423929<br>\r\n24,1435<br>\r\n24,1310872</p>\r\n<p>real 0m16.000s<br>\r\nuser 0m15.718s<br>\r\nsys 0m0.202s</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "11511": "",
    "11512": "",
    "11513": "",
    "11514": "",
    "11515": "",
    "11517": "",
    "11523": "",
    "11528": "",
    "11533": "",
    "11534": "",
    "11536": "",
    "11539": "",
    "11685": ""
  },
  "source": "meta"
}