Cuz, I did it unintentionally. After reading wikipedia I understand the "binary large object" is for large media files, and I'm not saving a media file.
So how does data get stored this way? What's wrong with this setup to display text as BLOB in phpmyadmin?
the MySql field from phpmyadmin,
Field = 'first_name'
Type = text
Collation = latin1_bin
Null = No
Default = None
The php code,
$insertName = "INSERT INTO name(first_name,last_name)VALUES('$firstName','$lastName')";
$dbSuccess_1 = mysql_query($insertName,$connectID) or die ("ERROR_1 - Unable to save
to MySQL".error_get_last().mysql_error($connectID));
If you're asking how to change a BLOB column to TEXT you would use a query similar to this:
ALTER TABLE `name`
CHANGE COLUMN `first_name` `first_name` TEXT NULL FIRST
,CHANGE COLUMN `last_name` `last_name` TEXT NULL AFTER `first_name`;
You can use PHPMyAdmin to make the change even easier.
TEXT and BLOB are essentially identical, except that TEXT fields are subject to character set limitations (and character set is taken into account while sorting/grouping the fields), while BLOBs are stored verbatim as a sequence of bytes and will not be transformed.
Relevant docs: http://dev.mysql.com/doc/refman/5.0/en/blob.html
Related
I am using php and intervention
What is the db format of a blob?
Is MySQL saving it as base64?
Before I save an image file to db what should I do?
Image::make('......')->encode('data-url');
Is that it?
How to store a Binary Large Object (BLOB)?
A BLOB is a binary large object that can hold a variable amount of
data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and
LONGBLOB.
These differ only in the maximum length of the values they can hold.
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and
LONGTEXT. These correspond to the four BLOB types and have the same
maximum lengths and storage requirements.
Hope the following code will help you:
CREATE TABLE IMAGE_TABLE(
IMG_ID INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
IMG_DETAILS CHAR(50),
IMG_DATA LONGBLOB,
IMG_NAME CHAR(50),
IMG_SIZE CHAR(50),
IMG_TYPE CHAR(50)
);
This will create a table which will suit your requirement.
You may also refer the following SO answers:
Binary Data in MySQL
store TEXT/BLOB in same table or not?
Storing messages as BLOB (Binary Large Object) or ordinary text?
You can refer the official documentation here. This link and this link would be worth a read to deepen your understanding.
I am creating a form that lets users to upload their photos/ video and it will be saved into my database. May I know is there a way to get this done? Im trying to save it in the database using item_path varchar which means it looks. Example: img/cats.jpg . What can I do to make it to be added to the database? Thank you
If you cannot create BLOB column types, then I would use a TEXT type with base64 encoding on the file data. You should take care to record the MIME type of the data and it would probably be a good idea to store a checksum (md5, or similar) as well.
Your files table might look something like this
-- files table
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
blob MEDIUMTEXT NOT NULL
mime_type VARCHAR(255) NOT NULL
checksum VARCHAR(255) NOT NULL
created_at DATETIME NOT NULL
You might have other columns that associate the file to a user or any other relevant information
Here are the size limits for the MySQL TEXT column types
TYPE SIZE LIMIT
----------------------------------------------------
TINYTEXT (2^8) 256 bytes
TEXT (2^16) 65,536 bytes (64 KiB)
MEDIUMTEXT (2^24) 16,777,216 bytes (16 MiB)
LONGTEXT (2^32) 4,294,967,296 bytes (4 GiB)
When you write to the DB using PHP, use the base64_encode function to encode the file.
If you cannot create TEXT column types, then you can forget saving the actual data in your database. You will then have to save the files to the filesystem and just store a link to the file in your database.
Or lastly, as another comment points out, you could use an external blob storage like Amazon S3 or other cloud pirate services.
I need to store a very big amount of text in mysql database. It will be millions of records with field type LONGTEXT and database size will be huge.
So, I want ask, if there is a safe way to compress text before storing it into TEXT field to save space, with ability to extract it back if needed?
Something like:
$archived_text = compress_text($huge_text);
// saving $archived_text to database here
// ...
// ...
// getting compressed text from database
$archived_text = get_text_from_db();
$huge_text = uncompress_text($archived_text);
Is there a way to do this with php or mysql? All the texts are utf-8 encoded.
UPDATE
My application is a large literature website where users can add their texts. Here is the table I have:
CREATE TABLE `book_parts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`book_id` int(11) NOT NULL,
`title` varchar(200) DEFAULT NULL,
`content` longtext,
`order_num` int(11) DEFAULT NULL,
`views` int(10) unsigned DEFAULT '0',
`add_date` datetime DEFAULT NULL,
`is_public` tinyint(3) unsigned NOT NULL DEFAULT '1',
`published_as_draft` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `key_order_num` (`order_num`),
KEY `add_date` (`add_date`),
KEY `key_book_id` (`book_id`,`is_public`,`order_num`),
CONSTRAINT FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Currently it has about 800k records and weights 4 GB, 99% of queries are SELECT. I have all reasons to think that numbers increase diagrammatically. I wouldn't like to store texts in the files because there is quite heavy logic around and my website has quite a few hits.
Are you going to index these texts. How big is read load on this texts? Insert load?
You can use InnoDB data compression - transparent and modern way. See docs for more info.
If you have realy huge texts (say, each text is above 10MB), than good idea is not to store them in Mysql. Store compressed by gzip texts in file system and only pointers and meta in mysql. You can easily expand your storage in future and move it to e.g. DFS.
Update: another plus of storing texts outside Mysql: DB stays small and fast. Minus: high probability of data inconsistence.
Update 2: if you have much programming resourses, please, take a look on projects like this one: http://code.google.com/p/mysql-filesystem-engine/.
Final Update: according to your info, you can just use InnoDB compression - it is the same as ZIP. You can start with these params:
CREATE TABLE book_parts
(...)
ENGINE=InnoDB
ROW_FORMAT=COMPRESSED
KEY_BLOCK_SIZE=8;
Later you will need to play with KEY_BLOCK_SIZE. See SHOW STATUS LIKE 'COMPRESS_OPS_OK' and SHOW STATUS LIKE 'COMPRESS_OPS'. Ratio of these two params must be close to 1.0: Docs.
If you're compressing (eg. gzip), then don't use TEXT fields of any sort. They're not binary-safe. Data going into/coming out of text fields is subject to character set translation, which probably (though not necessarily) mangle the compressed data and give you a corrupted result when you retrieve/uncompress the text.
Use BLOB fields instead, which are binary-transparent and do not to any translation of the data.
It might be better to define the text field as blob, and compress the data in PHP to save costs in communication.
CREATE TABLE book_parts (
......
content blob default NULL,
......
)
In PHP, use gzcompress and gzuncompress.
$content = '......';
$query = sprintf("replace into book_parts(content) values('%s') ",
mysql_escape_string(gzcompress($content)) );
mysql_query($query);
$query = "select * from book_parts where id = 111 ";
$result = mysql_query($query);
if ($result && $row = mysql_fetch_assoc($result))
$content = gzuncompress($row['content']);
You may also want to use a COMPRESS option to enable compression of packets.
Read some information about this option:
Use Compression in MySQL Connector/Net
Compress Property in dotConnect for MySQL
For PHP I have found this - MYSQLI_CLIENT_COMPRESS for mysqli_real_connect function.
You could use php functions gzdeflate and gzinflate for text.
There are no benefits in compressing large
texts into a database.
Here are the problems you might face in the long run:
If the server crashes the data may be hard to recover.
Not ideal for search.
It takes additional time to transfer the data between the mysql server and the browser.
Time consuming for backup (not using replication).
I think storing these large texts into a disk file will be easier for:
Distributed backup (rsync).
PHP to handle file upload.
I am in the process of migrating a large amount of data from several databases into one. As an intermediary step I am copying the data to a file for each data type and source db and then copying it into a large table in my new database.
The structure is simple in the new table, called migrate_data. It consists of an id (primary key), a type_id (incremented within the data type set), data (a field containing a serialized PHP object holding the data I am migrating), source_db (refers to the source database, obviously), data_type (identifies what type of data we are looking at).
I have created keys and key combinations for everything but the data field. Currently I have the data field set as a longtext column. User inserts are taking about 4.8 seconds each on average. I was able to trim that down to 4.3 seconds using DELAY_KEY_WRITE=1 on the table.
What I want to know about is whether or not there is a way to improve the performance even more. Possibly by changing to a different data column type. That is why I ask about the longtext vs text vs blob. Are any of those more efficient for this sort of insert?
Before you answer, let me give you a little more information. I send all of the data to an insert function that takes the object, runs it through serialize, then runs the data insert. It is also being done using Drupal 6 (and its db_query function).
Any efficiency improvements would be awesome.
Current table structure:
CREATE TABLE IF NOT EXISTS `migrate_data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type_id` int(10) unsigned NOT NULL DEFAULT '0',
`data` longtext NOT NULL,
`source_db` varchar(128) NOT NULL DEFAULT '',
`data_type` varchar(128) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `migrated_data_source` (`source_db`),
KEY `migrated_data_type_id` (`type_id`),
KEY `migrated_data_data_type` (`data_type`),
KEY `migrated_data_id__source` (`id`,`source_db`),
KEY `migrated_data_type_id__source` (`type_id`,`source_db`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 DELAY_KEY_WRITE=1;
The various text/blob types are all identical in storage requirements in PHP, and perform exactly the same way, except text fields are subject to character set conversion. blob fields are not. In other words, blobs are for when you're storing binary that MUST come out exactly the same as it went in. Text fields are for storing text data that may/can/will be converted from one charset to another.
What is difference between longblob and longtext in mysql?
What should I use to save a long topic ?
BLOBs are for Binary Large Objects. If you're storing binary data in your DB, then BLOB would be the table type you want. Otherwise.. longtext.
In your case longtext.
BLOB and TEXT are basically identical, except that TEXT fields have character set translation rules applied. BLOB fields do not. So with BLOB what you put in is what you get. With TEXT, what you put may not be what you get out.
You can use collation & character sets on TEXT columns, which would mean that:
If you specify a different charset for a connection than the TEXT column is (for instance,latin1 column, utf-8 requested), MySQL will convert the contents to the required charset.
You can sort & compare TEXT columns based on collation.
BLOB's are just 'binary sequences', and you'll get them 'as is'.