一切福田,不離方寸,從心而覓,感無不通。

Category Archives: MySQL

VS2015 +EF6 连接MYSQL数据库生成实体

使用EF设计器 此时此刻,发现二逼了,咋没有MySQL Database呢? 好吧,下面重点: 需要下载安装: 1:mysql-for-visualstudio-1.2.6.msi http://dev.mysql.com/downloads/file/?id=460645 2:mysql-connector-net-6.9.8.msi http://dev.mysql.com/downloads/connector/net/ 3:使用Nuget安装EF   我这边是已经安装完了(版本选择6.1以上的),所以显示“更新”和“卸载”,如果你安装了,继续看下面的   下图是随便找了一个没安装的,就会有“安装”按钮,   4:使用Nuget安装Mysql.Data.Entity 与安装EF相同 点击安装后,一会会出现如下图:     执行完上述步骤,看web.config文件,会自动增加如下代码 OK,现在我们所有的步骤就执行完了。最好重启下VS(不知道是不是必须,反正我重启了),之后重新编译。 再之后,就可以按开始的步骤生成MYSQL对应的实体了。 注:如果刚开始那两个MSI文件安装有问题,则生成实体的时候,到了这一步之后(如下图),会出现闪退问题,无法生成实体   (本人就遇到过这个问题) 到了这一步,已经没有任何悬念了。   来自为知笔记(Wiz) from:http://www.cnblogs.com/RushPasser/p/5438334.html

龙生   14 Jul 2016
View Details

mysql修改字段默认值

龙生   11 Nov 2015
View Details

mysql修改字段类型

龙生   11 Nov 2015
View Details

mysql添加字段及默认值

龙生   11 Nov 2015
View Details

在Mac如何启动MySQL

安装好MySQL服务后(安装步骤可以参考系列经验1)。打开“系统偏好设置”,单击下端的“MySQL”图标。 在“MySQL”对话框中,单击“启动MySQL服务”按钮。 在弹出的窗口中,输入管理员密码,然后单击“好”按钮。 在“MySQL”对话框中,MySQL服务的状态显示为:如下状态表示MySQL服务已经启动。 使用终端登录MySQL:  在Finder的侧边栏中单击“应用程序”,然后在“实用工具”中,双击启动“终端”命令。  在终端中输入添加MySQL路径的命令: PATH="$PATH":/usr/local/mysql/bin  在终端登录到MySQL的命令如下:mysql -u root -p 然后输入密码,如果没有设置密码,直接按enter键。  如果显示的内容如下,即是已经成功登录到MySQL服务。  

龙生   13 Sep 2015
View Details

PowerDesigner更改数据库类型

工具:powerdesigner

数据库:sql server2000 --> mysql 5.0

1、更改数据库

14207159_1

将DBMS里的数据库(原为sqlserver2000)改为mysql5.0

2、生成mysql脚本

Database --> Generate Database

在directory里选择生成的sql脚本存放的路径;

在file name里修改sql脚本的名字;

one file only 勾选后,只生成一个sql脚本;不勾选,按照表的个数生成多个脚本;

由于建模时是按sqlserver标准设计的,转成mysql会有错误,取消掉check model前面的勾可避免生成sql脚本时发生如下的错误:

Generation aborted due to errors detected during the verification of the model.

其他错误见结尾

为了避免错误,在此将create primary key改为inside

format选项卡里主要修改文本格式,如果有中文,改成UTF-8,避免生成的脚本有乱码;

在这个选项卡里选择workspace,点击旁边的include sub_objects,在下面的列表里会显示该工程下的所有表。点击select all,列表中的所有表前面会出现勾(也可以只选择几个表)。

点击确定,完成脚本生成。
———————————————————--

常见问题

自增问题: 解决方法如下:

在你所要设为自增型的键上(比如你的 id )双击,弹出一个 Column Properties 对话框,右下角有一个 Identify 的选择框,选中它 OK ,就可以了。

再去查看 Preview ,就能看到用大写标识出来的 AUTO_INCREMENT 。

右键表属性在physical options中可设置表类型MYISAM

设置字段默认值 :双击表,出现 column 列表,双击要设置的列的左边的灰色框,应该会弹出新的窗口,然后在新窗口上选择 standard   checks   ,里面有 default 的默认值

mysql in 子查询 效率慢 优化(转)

现在的CMS系统、博客系统、BBS等都喜欢使用标签tag作交叉链接,因此我也尝鲜用了下。但用了后发现我想查询某个tag的文章列表时速度很慢,达到5秒之久!百思不解(后来终于解决),我的表结构是下面这样的,文章只有690篇。

文章表article(id,title,content)
标签表tag(tid,tag_name)
标签文章中间表article_tag(id,tag_id,article_id)
其中有个标签的tid是135,我帮查询标签tid是135的文章列表
用以下语句时发现速度好慢,我文章才690篇
select id,title from article where id in(
select article_id from article_tag where tag_id=135
)
其中这条速度很快:select article_id from article_tag where tag_id=135
查询结果是五篇文章,id为428,429,430,431,432
我用写死的方式用下面sql来查文章也很快
select id,title from article where id in(
428,429,430,431,432
)
我在SqlServer中好像不会这样慢,不知MySQL怎样写好点,也想不出慢在哪里。

后来我找到了解决方法:

select id,title from article where id in(
select article_id from (select article_id from article_tag where tag_id=135) as tbt
)

 

 

其它解决方法:(举例)

mysql> select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');

为了节省篇幅,省略了输出内容,下同。

67 rows in set (12.00 sec)

只有67行数据返回,却花了12秒,而系统中可能同时会有很多这样的查询,系统肯定扛不住。用desc看一下(注:explain也可)

mysql> desc select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');
+—-+——————--+——————+——--+—————--+——-+———+————+———+————————--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+——————--+——————+——--+—————--+——-+———+————+———+————————--+
| 1 | PRIMARY | abc_number_prop | ALL | NULL | NULL | NULL | NULL | 2679838 | Using where |
| 2 | DEPENDENT SUBQUERY | abc_number_phone | eq_ref | phone,number_id | phone | 70 | const,func | 1 | Using where; Using index |
+—-+——————--+——————+——--+—————--+——-+———+————+———+————————--+
2 rows in set (0.00 sec)

从上面的信息可以看出,在执行此查询时会扫描两百多万行,难道是没有创建索引吗,看一下

mysql>show index from abc_number_phone;
+——————+————+————-+————--+—————--+———--+————-+———-+——--+——+————+———+—————+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+——————+————+————-+————--+—————--+———--+————-+———-+——--+——+————+———+—————+
| abc_number_phone | 0 | PRIMARY | 1 | number_phone_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 0 | phone | 1 | phone | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 0 | phone | 2 | number_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | number_id | 1 | number_id | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | created_by | 1 | created_by | A | 36879 | NULL | NULL | | BTREE | | |
| abc_number_phone | 1 | modified_by | 1 | modified_by | A | 36879 | NULL | NULL | YES | BTREE | | |
+——————+————+————-+————--+—————--+———--+————-+———-+——--+——+————+———+—————+
6 rows in set (0.06 sec)

mysql>show index from abc_number_prop;
+—————--+————+————-+————--+—————-+———--+————-+———-+——--+——+————+———+—————+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+—————--+————+————-+————--+—————-+———--+————-+———-+——--+——+————+———+—————+
| abc_number_prop | 0 | PRIMARY | 1 | number_prop_id | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | number_id | 1 | number_id | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | created_by | 1 | created_by | A | 311268 | NULL | NULL | | BTREE | | |
| abc_number_prop | 1 | modified_by | 1 | modified_by | A | 311268 | NULL | NULL | YES | BTREE | | |
+—————--+————+————-+————--+—————-+———--+————-+———-+——--+——+————+———+—————+
4 rows in set (0.15 sec)

从上面的输出可以看出,这两张表在number_id字段上创建了索引的。

看看子查询本身有没有问题。

mysql> desc select number_id from abc_number_phone where phone = '82306839';
+—-+————-+——————+——+—————+——-+———+——-+——+————————--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——————+——+—————+——-+———+——-+——+————————--+
| 1 | SIMPLE | abc_number_phone | ref | phone | phone | 66 | const | 6 | Using where; Using index |
+—-+————-+——————+——+—————+——-+———+——-+——+————————--+
1 row in set (0.00 sec)

没有问题,只需要扫描几行数据,索引起作用了。查询出来看看

mysql> select number_id from abc_number_phone where phone = '82306839';
+———--+
| number_id |
+———--+
| 8585 |
| 10720 |
| 148644 |
| 151307 |
| 170691 |
| 221897 |
+———--+
6 rows in set (0.00 sec)

直接把子查询得到的数据放到上面的查询中

mysql> select * from abc_number_prop where number_id in (8585, 10720, 148644, 151307, 170691, 221897);

67 rows in set (0.03 sec)

速度也快,看来MySQL在处理子查询的时候是不够好。我在MySQL 5.1.42 和 MySQL 5.5.19 都进行了尝试,都有这个问题。

搜索了一下网络,发现很多人都遇到过这个问题:

参考资料1:使用连接(JOIN)来代替子查询(Sub-Queries) mysql优化系列记录
http://blog.csdn.net/hongsejiaozhu/article/details/1876181
参考资料2:网站开发日记(14)-MYSQL子查询和嵌套查询优化
http://dodomail.iteye.com/blog/250199

根据网上这些资料的建议,改用join来试试。

修改前:select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');

修改后:select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';

mysql> select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';

67 rows in set (0.00 sec)

效果不错,查询所用时间几乎为0。看一下MySQL是怎么执行这个查询的

mysql>desc select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';
+—-+————-+——-+——+—————--+———--+———+—————--+——+————————--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————--+———--+———+—————--+——+————————--+
| 1 | SIMPLE | b | ref | phone,number_id | phone | 66 | const | 6 | Using where; Using index |
| 1 | SIMPLE | a | ref | number_id | number_id | 4 | eap.b.number_id | 3 | |
+—-+————-+——-+——+—————--+———--+———+—————--+——+————————--+
2 rows in set (0.00 sec)

小结:当子查询速度慢时,可用JOIN来改写一下该查询来进行优化。

网上也有文章说,使用JOIN语句的查询不一定总比使用子查询的语句快。

参考资料3:改变了对Mysql子查询的看法
http://hi.baidu.com/yzx110/blog/item/e694f536f92075360b55a92b.html

 

 

mysql手册也提到过,具体的原文在mysql文档的这个章节:

I.3. Restrictions on Subqueries

13.2.8. Subquery Syntax

摘抄:

1)关于使用IN的子查询:

Subquery optimization for IN is not as effective as for the = operator or for IN(value_list) constructs.

A typical case for poor IN subquery performance is when the subquery returns a small number of rows but the outer query returns a large number of rows to be compared to the subquery result.

The problem is that, for a statement that uses an IN subquery, the optimizer rewrites it as a correlated subquery. Consider the following statement that uses an uncorrelated subquery:

The optimizer rewrites the statement to a correlated subquery:

If the inner and outer queries return M and N rows, respectively, the execution time becomes on the order of O(M×N), rather than O(M+N) as it would be for an uncorrelated subquery.

An implication is that an IN subquery can be much slower than a query written using an IN(value_list) construct that lists the same values that the subquery would return.

2)关于把子查询转换成join的:

The optimizer is more mature for joins than for subqueries, so in many cases a statement that uses a subquery can be executed more efficiently if you rewrite it as a join.

An exception occurs for the case where an IN subquery can be rewritten as a SELECT DISTINCT join. Example:

That statement can be rewritten as follows:

But in this case, the join requires an extra DISTINCT operation and is not more efficient than the subquery

from:http://www.cnblogs.com/xh831213/archive/2012/05/09/2491272.html

使用MySQL正则表达式

正则表达式作用是匹配方本,将一个模式(正则表达式)与一个文本串进行比较。 MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据。 MySQL仅支持多数正则表达式实现的一个很小的子集。 ———————- 9.2.1  基本字符匹配 REGEXP后所跟的东西作为正则表达式处理。 SELECT prod_name FROM products WHERE prod_name REGEXP '1000' ORDER BY prod_name; ——返回—— +————————+ | prod_name | +————————+ | JetPack 1000 | +————————+     .  表示匹配任意一个字符。 SELECT prod_name FROM products WHERE prod_name REGEXP '.000' ORDER BY prod_name; ————返回———-- +————————-+ | prod_name | +————————-+ | JetPack 1000 | | JetPack 2000 | +————————-+   MySQL中的正则表达式匹配不区分大小写。 为区分大小写,可使用BINARY关键字。 如:WHERE prod_name REGEXP BINARY 'JetPack .000'   9.2.2  进行OR匹配 为搜索两个串之一(或者这个串,或者为另一个串),使用 | 。 | 作为OR操作符,表示匹配其中之一。可给出两个以上的OR条件。 SELECT prod_name FROM products WHERE prod_name REGEXP '1000 | 2000' ORDER BY […]

龙生   04 Jun 2015
View Details

解决:Entity Framework + MariaDb(MySql)中文乱码

今天写一MVC4+Entity Framework+Mysql的小例子时,发现中文写到数据库里是N个问号(乱码哦~); 于是跟了一下代码,发现页面提交过来的数据正常,这说明肯定是EF写到数据库时出了问题。 为了进一步验证,我用SQLServer2008试了一下,一切正常,那就在Mysql上找原因吧~ 但mysql库编码也是utf8,表也是utf8,这是什么原因呢? 百度了一把,看 了几篇文章也没能解决我的问题,无非都是要把网页、数据库、EF的编码改一致,我的本来都是一致的,难道是字段的编码?但字段的编码怎么改呢?也没找到方法,忽然看到字段的排序项是空的,会不会是它的原因呢?于是改成utf8——测试——一切正常~有图有真相:

龙生   14 May 2015
View Details

让Entity Framework支持MySql数据库

Entity Framework 4.0 也可以支持大名鼎鼎的MySql,这篇POST将向展示如何实现EF+MySql数据库的结构.首先,你必须下载 MySQL Connector/NET 6.3.5 ,也就是.net下连接MySql数据库的驱动程序. 还是像以往一样的,增加一个Entity data model: 将下面选择DataSource, 选择MySQL database 做为数据源. 接下来几步和以前一样,选择要Mapping的Tables: 好了,让我们来测试一下:

  可以工作.就是这么简单. 另一个消息是Entity Framework Code-First (CTP5 发布了). 如果您有兴趣可以看一下. from:http://www.cnblogs.com/wintersun/archive/2010/12/12/1903861.html

龙生   08 May 2015
View Details
1 17 18 19 22