我想做一个数据非规范化以获得更好的性能,并在Post模型中收到我的博客帖子收到的一些投票:
class Post(models.Model):
""" Blog entry """
author = models.ForeignKey(User)
title = models.CharField(max_length=255)
text = models.TextField()
rating = models.IntegerField(default=0) # here is the sum of votes!
class Vote(models.Model):
""" Vote for blog entry """
post = models.ForeignKey(Post)
voter = models.ForeignKey(User)
value = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
当然,我需要保持Post.rating实际价值.Nornally我会使用数据库触发器,但现在我决定发出一个post_save信号(减少数据库处理时间):
# vote was saved
@receiver(post_save, sender=Vote)
def update_post_votes(sender, instance, created, **kwargs):
""" Update post rating """
if created:
instance.post.rating += instance.value
instance.post.save()
else:
# if vote was updated, we need to remove …Run Code Online (Sandbox Code Playgroud) 在文献中,SQL查询效率通常以"查询前置"(QPS)来衡量.考虑到我手边有生产数据的数据库,这些措施是如何制定的?有没有MySQL工具可以做到这一点?
我正在使用django的调试工具栏,如果两个条件为真,我想将它添加到项目中:
settings.DEBUG 是 True做第一个并不难
# adding django debug toolbar
if DEBUG:
MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)
但是如何检查模块是否存在?
我找到了这个解决方案:
try:
import debug_toolbar
except ImportError:
pass
Run Code Online (Sandbox Code Playgroud)
但由于导入发生在django的其他地方,我需要if/else逻辑来检查模块是否存在,所以我可以在settings.py中检查它
def module_exists(module_name):
# ??????
# adding django debug toolbar
if DEBUG and module_exists('debug_toolbar'):
MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我有一个庞大而繁重的mysql数据库,有时执行速度非常快,但有时会变得非常慢.所有表都是InnoDB,服务器有32GB的RAM,数据库大小约为40GB.
前20个查询我slow_query_logARE update,insert以及delete查询和我不明白他们为什么这么慢(长达120秒,有时!)
这是最常见的查询:
UPDATE comment_fallows set comment_cnt_new = 0 WHERE user_id = 1;
Run Code Online (Sandbox Code Playgroud)
分析结果:
mysql> set profiling = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> update comment_fallows set comment_cnt_new = 0 where user_id = 1;
Query OK, 0 rows affected (2.77 sec)
Rows matched: 18 Changed: 0 Warnings: 0
mysql> show profile for query 1;
+---------------------------+----------+
| Status | Duration |
+---------------------------+----------+
| starting …Run Code Online (Sandbox Code Playgroud) 我想知道Python中是否有类似PHP natsort函数的东西?
l = ['image1.jpg', 'image15.jpg', 'image12.jpg', 'image3.jpg']
l.sort()
Run Code Online (Sandbox Code Playgroud)
得到:
['image1.jpg', 'image12.jpg', 'image15.jpg', 'image3.jpg']
Run Code Online (Sandbox Code Playgroud)
但我想得到:
['image1.jpg', 'image3.jpg', 'image12.jpg', 'image15.jpg']
Run Code Online (Sandbox Code Playgroud)
UPDATE
解决方案基于此链接
def try_int(s):
"Convert to integer if possible."
try: return int(s)
except: return s
def natsort_key(s):
"Used internally to get a tuple by which s is sorted."
import re
return map(try_int, re.findall(r'(\d+|\D+)', s))
def natcmp(a, b):
"Natural string comparison, case sensitive."
return cmp(natsort_key(a), natsort_key(b))
def natcasecmp(a, b):
"Natural string comparison, ignores case."
return natcmp(a.lower(), b.lower())
l.sort(natcasecmp);
Run Code Online (Sandbox Code Playgroud) 我想要:
Run Code Online (Sandbox Code Playgroud)Here is link: http://google.com And http://example.com inside. And another one at the very end: http://test.net
成为:
Run Code Online (Sandbox Code Playgroud)Here is link: <a href="http://google.com">http://google.com</a> And <a href="http://example.com">http://example.com</a> inside. And another one at the very end: <a href="http://test.net">http://test.net</a>
看起来像一个简单的任务,但我找不到一个有效的PHP函数.你有什么想法?
function make_links_clickable($text){
// ???
}
$text = 'Here is link: http://google.com
And http://example.com inside.
And another one at the very end: http://test.net';
echo make_links_clickable($text);
Run Code Online (Sandbox Code Playgroud) 有时,在PHP编码时,我们得到解析或语法错误,如下所示:
Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in /var/www/example/index.php on line 4
Run Code Online (Sandbox Code Playgroud)
我想知道,如果有一个PHP解释器可以输出的所有可能错误的列表.我搜索过php.net,但找不到这样的东西.我需要这个列表用于学术目的.
我有一个ubuntu服务器和一个相当高的网站.服务器是:
根据top命令,每个php-fpm进程消耗大约65MB的RAM :

空闲记忆:
admin@myserver:~$ free -m
total used free shared buffers cached
Mem: 7910 7156 753 0 284 2502
-/+ buffers/cache: 4369 3540
Swap: 8099 0 8099
Run Code Online (Sandbox Code Playgroud)
问题
最近,我遇到了很大的性能问题.非常大的响应时间,非常多Gateway Timeouts,在晚上,当负载变高时,90%的用户只看到"找不到服务器"而不是网站(我似乎无法重现这一点)
日志
我的Nginx错误日志充满了以下消息:
2012/07/18 20:36:48 [error] 3451#0: *241904 upstream prematurely closed connection while reading response header from upstream, client: 178.49.30.245, server: example.net, request: request: "GET /readarticle/121430 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "example.net", referrer: "http://example.net/articles"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过切换到unix socket,但仍然会遇到这些错误:
2012/07/18 19:27:30 [crit] 2275#0: *12334 connect() to …Run Code Online (Sandbox Code Playgroud) 我有两张桌子:
CREATE TABLE `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(1000) DEFAULT NULL,
`last_updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `last_updated` (`last_updated`),
) ENGINE=InnoDB AUTO_INCREMENT=799681 DEFAULT CHARSET=utf8
CREATE TABLE `article_categories` (
`article_id` int(11) NOT NULL DEFAULT '0',
`category_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`article_id`,`category_id`),
KEY `category_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
Run Code Online (Sandbox Code Playgroud)
这是我的查询:
SELECT a.*
FROM
articles AS a,
article_categories AS c
WHERE
a.id = c.article_id
AND c.category_id = 78
AND a.comment_cnt > 0
AND a.deleted …Run Code Online (Sandbox Code Playgroud) 当我和你愚蠢没什么经验时,我决定用PHP生成时间戳并将它们存储INT在我的MySQL innodb表的列中是个好主意.现在,当此表具有数百万条记录并需要一些基于日期的查询时,是时候将此列转换为TIMESTAMP.我该怎么做呢?
Currenlty,我的桌子看起来像这样:
id (INT) | message (TEXT) | date_sent (INT)
---------------------------------------------
1 | hello? | 1328287526
2 | how are you? | 1328287456
3 | shut up | 1328234234
4 | ok | 1328678978
5 | are you... | 1328345324
Run Code Online (Sandbox Code Playgroud)
以下是我提出的查询,将date_sent列转换为TIMESTAMP:
-- creating new column of TIMESTAMP type
ALTER TABLE `pm`
ADD COLUMN `date_sent2` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP();
-- assigning value from old INT column to it, in …Run Code Online (Sandbox Code Playgroud) mysql ×4
php ×3
python ×3
django ×2
performance ×2
innodb ×1
join ×1
linux ×1
natsort ×1
nginx ×1
postgresql ×1
preg-replace ×1
regex ×1
replication ×1
sorting ×1
sql ×1
timestamp ×1