<p>I really like Xml for saving data, but when does sqlite/database become the better option? eg, when the xml has more than <em>x</em> items or is greater than <em>y</em> MB?</p>

<p>I am coding an rss reader and I believe I made the wrong choice in using xml over a sqlite database to store a cache of <em>all</em> the feeds items. There are some feeds which have an xml file of ~1mb after a month, another has over 700 items, while most only have ~30 items and are ~50kb in size after a <em>several</em> months. </p>

<p>I currently have no plans to implement a cap because I like to be able to search through everything.</p>

<p>So, my questions are:</p>

<ol>
<li>When is the overhead of sqlite/databases justified over using xml?</li>
<li>Are the <strong>few large xml files</strong> justification enough for the database when there are <strong>a lot of small</strong> ones, though even the small ones will grow over time? (a long <em>long</em> time)</li>
</ol>

<p><strong>updated</strong> (more info)</p>

<p>Every time a feed is selected in the GUI I reload all the items from that feeds xml file.</p>

<p>I also need to modify the read/unread status which seems really hacky when I loop through all nodes in the xml to find the item and then set it to read/unread.</p>

[{'answer_id': 77750, 'author': 'Mitchel Sellers', 'author_id': 13279, 'author_profile': 'https://Stackoverflow.com/users/13279', 'pm_score': 2, 'selected': False, 'text': '<p>To me it really depends on what you are doing with them, how many users/processes need access to them at the same time etc.</p>\n\n<p>I work with large XML files all the time, but they are single process, import style items, that multi-user, or performance are not really needs.</p>\n\n<p>SO really it is a balance.</p>\n'}, {'answer_id': 77773, 'author': 'Sebastian Redl', 'author_id': 8922, 'author_profile': 'https://Stackoverflow.com/users/8922', 'pm_score': 0, 'selected': False, 'text': "<p>I say it's not a matter of data size, but of data type. If your data is <em>structured</em>, use a relational database. If your data is <em>semi-structured</em>, use XML or - if the data amounts really grow too large - an XML database.</p>\n"}, {'answer_id': 77777, 'author': 'typicalrunt', 'author_id': 13996, 'author_profile': 'https://Stackoverflow.com/users/13996', 'pm_score': 3, 'selected': False, 'text': "<p>I wouldn't use XML for storing RSS items.  A feed reader makes constant updates as it receives data.</p>\n\n<p>With XML, you need to load the data from file first, parse it, then store it for easy search/retrieval/update.  Sounds like a database...</p>\n\n<p>Also, what happens if your application crashes?  if you use XML, what state is the data in the XML file versus the data in memory.  At least with SQLite you get atomicity, so you are assured that your application will start with the same state as when the last database write was made.</p>\n"}, {'answer_id': 77778, 'author': 'Bradley Harris', 'author_id': 10503, 'author_profile': 'https://Stackoverflow.com/users/10503', 'pm_score': 3, 'selected': False, 'text': '<p>XML is best used as an interchange format when you need to move data from your application to somewhere else or share information between applications.  A database should be the preferred method of storage for almost any size application.  </p>\n'}, {'answer_id': 77812, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>If your searching go with a db.  You could split the xml files up into directories to ease seeking, but the managerial overhead easily gets quite heavy.  You also get a lot more than just performance with a sql db... </p>\n'}, {'answer_id': 77837, 'author': 'apenwarr', 'author_id': 42219, 'author_profile': 'https://Stackoverflow.com/users/42219', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with @Bradley.</p>\n\n<p>XML is very slow and not particularly useful as a storage format.  Why bother?  Will you be editing the data by hand using a text editor?  If so, XML <em>still</em> isn\'t a very convenient format compared to something like YAML.  With something like SQlite, queries are easier to write, and there\'s a well defined API for getting your data in and out.</p>\n\n<p>XML is fine if you need to send data around between programs.  But in the name of efficiency, you should probably produce the XML at sending time, and parse it into "real data" at receive time.</p>\n\n<p>All the above means that your question about "when the overhead of a database is justified" is kind of moot.  XML has a way higher overhead, all the time, than SQlite does.  (Full-on databases like MSSQL are heavier, especially in administrative overhead, but that\'s a totally different question.)</p>\n'}, {'answer_id': 77897, 'author': 'Vin', 'author_id': 1747, 'author_profile': 'https://Stackoverflow.com/users/1747', 'pm_score': 3, 'selected': False, 'text': '<ol>\n<li>Use XML for data that the\napplication should know -\nconfiguration, logging and what not.</li>\n<li>Use databases(oracle, SQL server etc) for data that the user\ninteracts with directly or\nindirectly - real data</li>\n<li>Use SQLite if the user data is more\nof a serialized collection - like\nhuge list of files and their content\nor collection of email items etc.\nSQLite is good at that.</li>\n</ol>\n\n<p>Depends on the kind and the size of the data.</p>\n'}, {'answer_id': 77903, 'author': 'Mostlyharmless', 'author_id': 12881, 'author_profile': 'https://Stackoverflow.com/users/12881', 'pm_score': 2, 'selected': False, 'text': '<p>If any time you will need to scale, use databases.</p>\n'}, {'answer_id': 77912, 'author': 'Oli', 'author_id': 12870, 'author_profile': 'https://Stackoverflow.com/users/12870', 'pm_score': 4, 'selected': False, 'text': "<p>Don't forget that you have a great database at your fingertips: the filesystem!</p>\n\n<p>Lots of programmers forget that a decent directory-file structure is/has:</p>\n\n<ol>\n<li>It's fast as hell</li>\n<li>It's portable</li>\n<li>It has a tiny runtime footprint</li>\n</ol>\n\n<p>People are talking about splitting up XML files into multiple XML files... I would consider splitting your XML into multiple directories and multiple plaintext files. </p>\n\n<p>Give it a go. It's refreshingly fast.</p>\n"}, {'answer_id': 78026, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>XML is good for storing data which is not completely structured and you typically want to exchange it with another application. I prefer to use a SQL database for data.  XML is error prone as you can cause subtle errors due to typos or ommissions in the data itself. Some open source application frameworks use too many xml files for configuration, data, etc. I prefer to have it in SQL.</p>\n\n<p>Since you ask for a rule of thumb, I would say that use XML based application data, configuration, etc if you are going to set it up once and not access/search it much. For active searches and updations, its best to go with SQL.</p>\n\n<p>For example, a web server stores application data in a XML file and you dont really need to perform complex search, update the file. The web server starts, reads the xml file and thats that. So XML is perfect here.  Suppose you use a framework like Struts. You need to use XML and the action configurations dont change much once the application is developed and deployed. So again, the XML file is a good way.  Now if your Struts developed application allows extensive searches and updations, deletions, then SQL is the optimal way.</p>\n\n<p>Offcourse, you will surely meet one or two developers in your organisation who will chant XML or SQL only and proclaim XML or SQL as the only way to go. Beware of such folks and do what 'feels' right for your application. Dont just follow a 'technology religion'.</p>\n\n<p>Think of things like how often you need to update the data, how often you need to search the data. Then you will have your answer on what to use - XML or SQL. </p>\n"}, {'answer_id': 78040, 'author': 'Stan', 'author_id': 14091, 'author_profile': 'https://Stackoverflow.com/users/14091', 'pm_score': 6, 'selected': True, 'text': '<p>I basically agree with <a href="https://stackoverflow.com/questions/77726/xml-or-sqlite-when-to-drop-xml-for-a-database#77750">Mitchel</a>, that this can be highly specific depending on what are you going to do with XML and SQLite. For your case (cache), it seems to me that using SQLite (or other embedded databases) makes more sense.</p>\n<p>First I don\'t really think that SQLite will need more overhead than XML. And I mean both development time overhead and runtime overhead. Only problem is that you have a dependence on SQLite library. But since you would need some library for XML anyway it doesn\'t matter (I assume project is in C/C++).</p>\n<p><strong>Advantages of SQLite over XML:</strong></p>\n<ul>\n<li>everything in one file,</li>\n<li>performance loss is lower than XML as cache gets bigger,</li>\n<li>you can keep feed metadata separate from cache itself (other table), but accessible in the same way,</li>\n<li>SQL is probably easier to work with than XPath for most people.</li>\n</ul>\n<p><strong>Disadvantages of SQLite:</strong></p>\n<ul>\n<li>can be problematic with multiple processes accessing same database (probably not your case),</li>\n<li>you should know at least basic SQL. Unless there will be hundreds of thousands of items in cache, I don\'t think you will need to optimize it much,</li>\n<li>maybe in some way it can be more dangerous from security standpoint (SQL injection). On the other hand, you are not coding web app, so this should not happen.</li>\n</ul>\n<p>Other things are on par for both solutions probably.</p>\n<p>To sum it up, answers to your questions respectively:</p>\n<ol>\n<li><p>You will not know, unless you test your specific application with both back ends. Otherwise it\'s always just a guess. Basic support for both caches should not be a problem to code. Then benchmark and compare.</p>\n</li>\n<li><p>Because of the way XML files are organized, SQLite searches should always be faster (barring some corner cases where it doesn\'t matter anyway because it\'s blazingly fast). Speeding up searches in XML would require index database anyway, in your case that would mean having cache for cache, not a particularly good idea. But with SQLite you can have indexing as part of database.</p>\n</li>\n</ol>\n'}, {'answer_id': 78146, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>XML can be stored as text and as a binary file format. </p>\n\n<p>If your primary goal is to let a computer read / write a file format effeciently you should work with a binary file format. </p>\n\n<p>Databases are an easy to use way of storing and maintaining data. \nThey are not the fastest way to store data that is a binary file format. </p>\n\n<p>What can speed things up is using an in memory database / database type. Sqlite has this option. </p>\n\n<p>And this sounds like the best way to do it for you. </p>\n'}, {'answer_id': 78149, 'author': 'Jay Stramel', 'author_id': 3547, 'author_profile': 'https://Stackoverflow.com/users/3547', 'pm_score': 1, 'selected': False, 'text': "<p>My opinion is that you should use SQLite (or another appropriate embedded database) anytime you don't need a pure-text file format.  Note, this is a pretty big exception.  There are a lot of scenarios that require, or are benefited by, pure-text file formats.</p>\n\n<p>As far as overhead goes, SQLite compiles to something like 250 k with normal flags.  Many XML parsing libraries are larger than SQLite.  You get no concurrency gains using XML.  The SQLite binary file format is going to support much more efficient writes (largely because you can't append to the end of a well-formatted XML file).  And even reading data, most of which I assume is fairly random access, is going to be faster using SQLite.</p>\n\n<p>And to top it all off, you get access to the benefits of SQL like transactions and indexes.</p>\n\n<p>Edit:  Forgot to mention.  One benefit of SQLite (as opposed to many databases) is that it allows any type in any row in any column.  Basically, with SQLite you get the same freedom you have with XML in terms of datatypes.  This also means that you don't have to worry about putting limits on text columns.</p>\n"}, {'answer_id': 78871, 'author': 'David Medinets', 'author_id': 219658, 'author_profile': 'https://Stackoverflow.com/users/219658', 'pm_score': 3, 'selected': False, 'text': "<p>When should XML be used for data persistence instead of a database? Almost never. XML is a data transport language. It is slow to parse and awkward to query. Parse the XML (don't shred it!) and convert the resulting data into domain objects. Then persist the domain objects. A major advantage of a database for persistence is SQL which means unstructured queries and access to common tools and optimization techniques.</p>\n"}, {'answer_id': 78918, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>Man do I have experience with this. I work on a project where we originally stored all of our data using XML, then moved to SQLite. There are many pros and cons to each technology, but it was performance that caused the switchover. Here is what we observed.</p>\n<p>For small databases (a few meg or smaller), XML was much faster, and easier to deal with. Our data was naturally in a tree format, which made XML much more attractive, and XPath allowed us to do many queries in one simple line rather than having to walk down an ancestry tree.</p>\n<p>We were programming in a Win32 environment, and used the standard Microsoft DOM library. We would load all the data into memory, parse it into a DOM tree and search, add, modify on the in memory copy. We would periodically save the data, and needed to rotate copies in case the machine crashed in the middle of a write.</p>\n<p>We also needed to build up some &quot;indexes&quot; by hand using C++ tree maps. This, of course would be trivial to do with SQL.</p>\n<p>Note that the size of the data on the filesystem was a factor of 2-4 smaller than the &quot;in memory&quot; DOM tree.</p>\n<p>By the time the data got to 10M-100M size, we started to have real problems. Interestingly enough, at all data sizes, XML processing was much faster than SQLite turned out to be (because it was in memory, not on the hard drive)! The problem was actually twofold- first, loadup time really started to get long. We would need to wait a minute or so before the data was in memory and the maps were built. Of course once loaded the program was very fast. The second problem was that all of this memory was tied up all the time. Systems with only a few hundred meg would be unresponsive in other apps even though we ran very fast.</p>\n<p>We actually looking into using a filesystem based XML database. There are a couple open sourced versions XML databases, we tried them. I have never tried to use a commercial XML database, so I can't comment on them. Unfortunately, we could never get the XML databases to work well at all. Even the act of populating the database with hundreds of meg of XML took hours.... Perhaps we were using it incorrectly. Another problem was that these databases were pretty heavyweight. They required Java and had full client server architecture. We gave up on this idea.</p>\n<p>We found SQLite then. It solved our problems, but at a price. When we initially plugged SQLite in, the memory and load time problems were gone. Unfortunately, since all processing was now done on the harddrive, the background processing load went way up. While earlier we never even noticed the CPU load, now the processor usage was way up. We needed to optimize the code, and still needed to keep some data in memory. We also needed to rewrite many simple XPath queries as complicated multiquery algorithms.</p>\n<p>So here is a summary of what we learned.</p>\n<ol>\n<li><p>For tree data, XML is much easier to query and modify using XPath.</p>\n</li>\n<li><p>For small datasets (less than 10M), XML blew away SQLite in performance.</p>\n</li>\n<li><p>For large datasets (greater than 10M-100M), XML load time and memory usage became a big problem, to the point that some computers become unusable.</p>\n</li>\n<li><p>We couldn't get any opensource XML database to fix the problems associated with large datasets.</p>\n</li>\n<li><p>SQLite doesn't have the memory problems of XML DOM, but it is generally slower in processing the data (it is on the hard drive, not in memory). (note- SQLite tables can be stored in memory, perhaps this would make it as fast.... We didn't try this because we wanted to get the data out of memory.)</p>\n</li>\n<li><p>Storing and querying tree data in a table is not enjoyable. However, managing transactions and indexing partially makes up for it.</p>\n</li>\n</ol>\n"}, {'answer_id': 86688, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You should note that many large Relational DBs (Oracle and SQLServer) have XML datatypes to store data within a database and use XPath within the SQL statement to gain access to that data.</p>\n\n<p>Also, there are native XML databases which work very much like SQLite in the sense they are one binary file holding a collection of documents (which could roughly be a table) then you can either XPath/XQuery on a single document or the whole collection. So with an XML database you can do things like store the days data as a separate XML document in the collection... so you just need to use that one document when your dealing with the data for today. But write an XQuery to figure out historical data on the collection of documents for that person. Slick.</p>\n\n<p>I\'ve used Berkeley XMLDB (now backed by Oracle). There are others if you search google for "Native XML Database". I\'ve not seen a performance problem with storing/retrieving data in this manner. </p>\n\n<p>XQuery is a different beast (but well worth learning), however you may be able to just use the XPaths you currently use with slight modifications.</p>\n'}, {'answer_id': 86896, 'author': 'Martin Beckett', 'author_id': 10897, 'author_profile': 'https://Stackoverflow.com/users/10897', 'pm_score': 1, 'selected': False, 'text': "<p>A database is great as part of your program. If quering the data is part of your business logic.\nXML is best as a file format, especially if you data format is:</p>\n\n<p>1, Hierarchal <br>\n2, Likely to change in the future in ways you can't guess <br>\n3, The data is going to live longer than the program</p>\n"}, {'answer_id': 88358, 'author': 'sieben', 'author_id': 1147, 'author_profile': 'https://Stackoverflow.com/users/1147', 'pm_score': 3, 'selected': False, 'text': "<p>I have made the switch to SQLite and I feel <em>much</em> better knowing it's in a database. </p>\n\n<p>There are a lot of other benefits from this:  </p>\n\n<ul>\n<li>Adding new items is really simple</li>\n<li>Sorting by multiple columns</li>\n<li>Removing duplicates with a unique index</li>\n</ul>\n\n<p>I've created 2 views, one for unread items and one for all items, not sure if this is the best use of views, but I really wanted to try using them.</p>\n\n<p>I also benchmarked the xml vs sqlite using the <strong>StopWatch</strong> class, and the sqlite is faster, <strong>although it could just be that my way of parsing xml files wasn't the fastest method</strong>.</p>\n\n<ol>\n<li><strong>Small # items and size (25 items, 30kb)</strong>\n\n<ul>\n<li>~1.5 ms sqlite</li>\n<li>~8.0 ms xml</li>\n</ul></li>\n<li><strong>Large # of items (700 items, 350kb)</strong>\n\n<ul>\n<li>~20 ms sqlite</li>\n<li>~25 ms xml</li>\n</ul></li>\n<li><strong>Large file size (850 items, 1024kb)</strong>\n\n<ul>\n<li>~45 ms sqlite</li>\n<li>~60 ms xml</li>\n</ul></li>\n</ol>\n"}]
['https://Stackoverflow.com/questions/77726', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1147/']