Use MySQL database to support schemaless database storage solutions

Source: Internet
Author: User

On pycon, some kids shoes share similar concepts, but it is not suitable for general Internet projects. It feels a little too different. However, I implemented this solution before seeing pycon's sharing. The same requirement is different. Here, I only implement a data access component, not a server.

First, the method in this article is from how to use the MySQL database shared by friendfeed. In short, the python object is directly dumps and then zip compressed and stored in a MySQL field. Isn't schemaless enough? MySQL does not need to know what data types and classes are stored, and does not need to modify the database table structure when adding an attribute, it is no longer suitable for Internet businesses with fast business changes and growth. The access object is directly queried through the primary key, which is fast and direct. But, what should I do? Some children's shoes may ask. OK. The query score is two points. For simple retrieval, you can create an index table or use an external index, such as Lucent, you can also perform full-text search. Now, I have implemented the index table indexing method, because the external index method is quite strange, so you can write one as needed, you can implement several corresponding methods.

The preceding example is used to describe. Suppose you want to implement a blog and need to store the blog information. First, define a model class for the blog (What do you need to import automatically)

1 ClassBlog (dynamicbase ):
2Title = column (UNICODE, max_length = 200)
3Content = column (UNICODE)
4Post_date = column (datetime. datetime, db_index = true)
5Auther = fkcolumn (User)
6ClassMeta:
7Table_name ="Blogs"
8Connection = Connections [dB]

This connection is because I haven't thought about how to seamlessly integrate it into Django and take into account the temporary measures that can be used independently of Django. The complete version will be removed.

If you are using Django, you only need Python manage. py shell and then blog. Objects. create_table ()

At this time, the table and index table defined by the model are automatically created.

Data Table blogs:

Create Table 'blogs '(
'Id' int (11) not null,
'Object' varbinary (20000) not null,
Primary Key ('id ')
) Engine = InnoDB default charset = utf8;

Create two index tables at the same time

Create Table 'blog _ idx_post_date '(
'Id' int (10) unsigned not null,
'Post _ date' datetime not null,
Primary Key ('id '),
Index 'idx _ blogs_by_post_date '('Post _ date') using btree
) Engine = InnoDB default charset = utf8;

 

Create Table 'blog _ idx_auther '(
'Id' int (10) unsigned not null,
'Audio' int not null,
Primary Key ('id '),
Index 'idx _ blogs_by_auther '('auther') using btree
) Engine = InnoDB default charset = utf8;

 

At this time, you can use the blog. objects. create (Title = u "title", content = u "content", post_date = datetime. datetime. now (), auther = user) or blog. objects. create (Title = u "title", content = u "content", post_date = datetime. datetime. now (), auther = user. ID)

You can create a blog object. Data is inserted to both the blogs table and the two index tables. However, the object column in the blogs table is an object column that humans cannot understand ..........

Get the blog object directly by ID. objects. get (1), get the blog according to the index. objects. auther. query (auther = user. ID) or blog. objects. auther. query (auther = user)

This will generate an SQL statement, select 'id' from 'blog _ idx_auther 'where 'audio' = % s, retrieve the ID list of the matched object, traverse the ID list, and use the blog. objects. get (ID) Object List.

Blog. Objects. Get (ID) has an object cache (implemented through redis at this stage), so after testing, the speed is reliable. Compared with mangodb, Mysql Data Storage is more reliable. Therefore, compared with mangodb, which is currently not very reliable, the MySQL-based schemaless solution is relatively reliable.

 

As this item is still in the project incubator stage, it is not yet open-source for everyone to entertain, so please give more comments and suggestions on this solution,Source codeIt is estimated that the Chinese new year will bring people to the public.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.